decimal

Limit a double to two decimal places without trailing zeros

夙愿已清 提交于 2019-12-17 16:04:44
问题 I read this and that. I want this exactly: 1.4324 => "1.43" 9.4000 => "9.4" 43.000 => "43" 9.4 => "9.40" (wrong) 43.000 => "43.00" (wrong) In both questions the answers points to NSNumberFormatter . So it should be easy to achieve, but not for me. - (void)viewDidLoad { [super viewDidLoad]; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)]; NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init]; [doubleValueWithMaxTwoDecimalPlaces

Storing statistical data, do I need DECIMAL, FLOAT or DOUBLE?

无人久伴 提交于 2019-12-17 15:35:21
问题 I am creating for fun, but I still want to approach it seriously, a site which hosts various tests. With these tests I hope to collect statistical data. Some of the data will include the percentage of the completeness of the tests as they are timed. I can easily compute the percentage of the tests but I would like true data to be returned as I store the various different values concerning the tests on completion. Most of the values are, in PHP floats, so my question is, if I want true

Double vs Decimal Rounding in C#

柔情痞子 提交于 2019-12-17 14:47:10
问题 Why does: double dividend = 1.0; double divisor = 3.0; Console.WriteLine(dividend / divisor * divisor); output 1.0, but: decimal dividend = 1; decimal divisor = 3; Console.WriteLine(dividend / divisor * divisor); outputs 0.9999999999999999999999999999 ? I understand that 1/3 can't be computed exactly, so there must be some rounding. But why does Double round the answer to 1.0, but Decimal does not? Also, why does double compute 1.0/3.0 to be 0.33333333333333331? If rounding is used, then

Decimal values with thousand separator in Asp.Net MVC

三世轮回 提交于 2019-12-17 10:52:23
问题 I have a custom model class which contains a decimal member and a view to accept entry for this class. Everything worked well till I added javascripts to format the number inside input control. The format code format the inputted number with thousand separator ',' when focus blur. The problem is that the decimal value inside my modal class isn't bind/parsed well with thousand separator. ModelState.IsValid returns false when I tested it with "1,000.00" but it is valid for "100.00" without any

Convert hex to float

筅森魡賤 提交于 2019-12-17 10:52:03
问题 How to convert the following hex string to float (single precision 32-bit) in Python? "41973333" -> 1.88999996185302734375E1 "41995C29" -> 1.91700000762939453125E1 "470FC614" -> 3.6806078125E4 回答1: >>> import struct >>> struct.unpack('!f', '41973333'.decode('hex'))[0] 18.899999618530273 >>> struct.unpack('!f', '41995C29'.decode('hex'))[0] 19.170000076293945 >>> struct.unpack('!f', '470FC614'.decode('hex'))[0] 36806.078125 Update: see comment on how to do this in Python 3. 回答2: I recommend

Regex greater than zero with 2 decimal places

倖福魔咒の 提交于 2019-12-17 10:35:05
问题 I need a RegEx for a numeric value with up to two decimal places greater than zero and may or may not have a zero in the ones column. I should also add....whole numbers are fine. See somments below but there could be leading or trailing white spaces Good values: .1 0.1 1.12 123.12 92 092 092.13 Error values: 0 0.0 0.00 00 1.234 -1 -1.2 Anything less than zero 回答1: How about this: ^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$ Explanation: ^ # Start of string \s* # Optional whitespace (?=.*[1-9]) #

Why does adding two decimals in Javascript produce a wrong result? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-17 10:28:26
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is JavaScript’s Math broken? Why does JS screw up this simple math? console.log(.1 + .2) // 0.3000000000000004 console.log(.3 + .6) // 0.8999999999999999 The first example is greater than the correct result, while the second is less. ???!! How do you fix this? Do you have to always convert decimals into integers before performing operations? Do I only have to worry about adding (* and / don't appear to have the

Efficiently convert between Hex, Binary, and Decimal in C/C++

孤者浪人 提交于 2019-12-17 09:55:15
问题 I have 3 base representations for positive integer numbers: Decimal, in unsigned long variable (e.g. unsigned long int NumDec = 200 ). Hex, in string variable (e.g. string NumHex = "C8" ) Binary, in string variable (e.g. string NumBin = "11001000" ) I want to be able to convert between numbers in all 3 representations in the most efficient way. I.e. to implement the following 6 functions: unsigned long int Binary2Dec(const string & Bin) {} unsigned long int Hex2Dec(const string & Hex) {}

Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?

拈花ヽ惹草 提交于 2019-12-17 09:37:54
问题 It's important to note that I'm not looking for a rounding function. I am looking for a function that returns the number of decimal places in an arbitrary number's simplified decimal representation. That is, we have the following: decimalPlaces(5555.0); //=> 0 decimalPlaces(5555); //=> 0 decimalPlaces(555.5); //=> 1 decimalPlaces(555.50); //=> 1 decimalPlaces(0.0000005); //=> 7 decimalPlaces(5e-7); //=> 7 decimalPlaces(0.00000055); //=> 8 decimalPlaces(5.5e-7); //=> 8 My first instinct was to

Raising a decimal to a power of decimal?

别来无恙 提交于 2019-12-17 09:33:11
问题 The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function? 回答1: To solve my problem I found some expansion series, and them I had them implemented to solve the equation X^n = e^(n * ln x). // Adjust this to modify the precision public const int ITERATIONS = 27; // power