decimal

.NET decimal.ToString(format) in Javascript

我怕爱的太早我们不能终老 提交于 2019-12-24 00:44:02
问题 I have a string (€#,###.00) which works fine with aDecimal.ToString("€#,###.00") in .NET, i wonder if anyone knows how this could be achieved with javascript 回答1: There's .toLocaleString() , but unfortunately the specification defines this as "implementation dependant" - I hate when they do that. Thus, it behaves differently in different browsers: var val = 1000000; alert(val.toLocaleString()) // -> IE: "1,000,000.00" // -> Firefox: "1,000,000" // -> Chrome, Opera, Safari: "1000000" (i know,

Float point to Hex in C#

感情迁移 提交于 2019-12-24 00:42:01
问题 Googling I found there is not much information "to the point" on how to convert numbers to hexadecimal floating point single precision. There are three clear steps: 1 Convert entire binary part. 2 Add a comma and convert the fractional part to binary. 3 Put the result in scientific reporting. 4 Pass the result to the IEEE-754 standard 32 bits. This would result in binary. Then is turn it into hexadecimal. And all this is a bummer, I put the code hoping that it will solve that for me;-)

How to change decimal to binary and restore its bit values to an array?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 00:05:18
问题 For example: $result = func(14); The $result should be: array(1,1,1,0) How to implement this func ? 回答1: function func($number) { return str_split(decbin($number)); } 回答2: decbin would produce a string binary string: echo decbin(14); # outputs "1110" array_map('intval', str_split(decbin(14))) # acomplishes the full conversion 回答3: <?php function int_to_bitarray($int) { if (!is_int($int)) { throw new Exception("Not integer"); } return str_split(decbin($int)); } $result = int_to_bitarray(14);

Converting to binary in C++

笑着哭i 提交于 2019-12-23 22:06:49
问题 I made a function that converts numbers to binary. For some reason it's not working. It gives the wrong output. The output is in binary format, but it always gives the wrong result for binary numbers that end with a zero(at least that's what I noticed..) unsigned long long to_binary(unsigned long long x) { int rem; unsigned long long converted = 0; while (x > 1) { rem = x % 2; x /= 2; converted += rem; converted *= 10; } converted += x; return converted; } Please help me fix it, this is

PHP Division Float Value Issue

社会主义新天地 提交于 2019-12-23 19:28:16
问题 When I Try get remainder, It gives invalid value. I am trying to get remained of two decimal values and I get 3.4694469519536E-18 My Values are $x=0.1; $y=0.005; I tried the following: echo $ed = fmod(0.1,0.005); OutPut:3.4694469519536E-18 echo $ed = ((floatval(0.1)) % (floatval(0.005))); But getting Warning: Division by zero But Reality is 0.1 will be divide by 0.005 in 20 times. i.e. 0.1/0.005 = 20 The same function returns correct value when I use 10%1 = 0 . How can I get the exact

How to get decimal to n places precision in C# program for Pi

自闭症网瘾萝莉.ら 提交于 2019-12-23 17:29:37
问题 With reg to this question Pi in C# I coded the below code and gives a output with last 6 digits as 0. So I want to improve the program by converting everything to decimal. I have never used decimal in C# instead of a double before and I am only comfortable with double in my regular use. So please help me with decimal conversion, i tried to replace all double to decimal at start and it didnt turn good :(. using System; class Program { static void Main() { Console.WriteLine(" Get PI from

generatesDecimalNumbers for NumberFormatter does not work

风流意气都作罢 提交于 2019-12-23 17:24:54
问题 My function is converting a string to Decimal func getDecimalFromString(_ strValue: String) -> NSDecimalNumber { let formatter = NumberFormatter() formatter.maximumFractionDigits = 1 formatter.generatesDecimalNumbers = true return formatter.number(from: strValue) as? NSDecimalNumber ?? 0 } But it is not working as per expectation. Sometimes it's returning like Optional(8.300000000000001) Optional(8.199999999999999) instead of 8.3 or 8.2. In the string, I have value like "8.3" or "8.2" but the

How to format a Rational number as a decimal?

混江龙づ霸主 提交于 2019-12-23 17:16:26
问题 Given an arbitrary large (or small) Rational number that has a finite decimal representation, e.g.: r = Rational(1, 2**15) #=> (1/32768) How can I get its full decimal value as a string? The expected output for the above number is: "0.000030517578125" to_f apparently doesn't work: r.to_f #=> 3.0517578125e-05 And sprintf requires me to specify the number of digits: sprintf('%.30f', r) #=> "0.000030517578125000000000000000" 回答1: Most ten-year-olds know how to do that: use long division! 1 Code

What is the correct default value for a MySQL decimal field?

白昼怎懂夜的黑 提交于 2019-12-23 16:19:54
问题 I have a decimal field in my MySQL database. I have defined it as this: decimal(1,1) UNSIGNED NULL . But I would like to set a default value for it like 7.0 , and this is the problem I have. Whenever I want to set this value, I get this error: Invalid default value ... I also tried to set it as 7,0 and 7 but it resulted the same error. What is the correct default value for a MySQL decimal field? Note: I am using Navicat for MySQL 回答1: In MySQL, when declaring DECIMAL(P,S) : The precision (P)

C# decimal, how to add trailing zeros

假装没事ソ 提交于 2019-12-23 12:37:25
问题 I have to add trailing zeros to a decimal value. Not only for displaying (so Format is not an option), but in the actual underlying data, because the decimal precision is important in our application. I tried: decimal value = 1M decimal withPrecision = value + 0.000M; Which works well in many cases ... strangely not in all. I debugged into a case where the value in withPrecision was still 1M, by not seeing any difference in the value at runtime and the same hardcoded value in the immediate