decimal

How to convert this scientific notation to decimal?

余生长醉 提交于 2019-12-17 19:57:32
问题 After search in google, using below code still can not be compiled : decimal h = Convert.ToDecimal("2.09550901805872E-05"); decimal h2 = Decimal.Parse( "2.09550901805872E-05", System.Globalization.NumberStyles.AllowExponent); 回答1: You have to add NumberStyles.AllowDecimalPoint too: Decimal.Parse("2.09550901805872E-05", NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint); MSDN is clear about that: Indicates that the numeric string can be in exponential notation. The AllowExponent flag

Convert decimal to binary in C

╄→гoц情女王★ 提交于 2019-12-17 19:32:31
问题 I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn't work: void dectobin(int value, char* output) { int i; output[5] = '\0'; for (i = 4; i >= 0; --i, value >>= 1) { output[i] = (value & 1) + '0'; } } Any help would be much appreciated! 回答1: The value is not decimal. All values in computer's memory are binary. What you are trying to do is to convert int to a string using specific base. There's a function

Binary representation of a .NET Decimal

耗尽温柔 提交于 2019-12-17 19:17:57
问题 How does a .NET decimal type get represented in binary in memory? We all know how floating-point numbers are stored and the thusly the reasons for the inaccuracy thereof, but I can't find any information about decimal except the following: Apparently more accurate than floating-point numbers Takes 128 bits of memory 2^96 + sign range 28 (sometimes 29?) total significant digits in the number Is there any way I can figure this out? The computer scientist in me demands the answer and after an

Print number in engineering format

╄→尐↘猪︶ㄣ 提交于 2019-12-17 18:54:34
问题 I am trying to print a number into engineering format with python, but I cannot seem to get it to work. The syntax SEEMS simple enough, but it just doesn't work. >>> import decimal >>> x = decimal.Decimal(1000000) >>> print x 1000000 >>>> print x.to_eng_string() 1000000 I cannot figure out why this is. The two values are not equal (one is a string, the other is an int). Setting various contexts in decimal doesn't seem to help either. Any clues or ideas? 回答1: To get this to work, you have to

How would I separate thousands with space in C#

拥有回忆 提交于 2019-12-17 18:22:32
问题 Assume I have the following decimal number that I have to format so that every thousand should be separated with a space: 897.11 to 897.11 1897.11 to 1 897.11 12897.11 to 12 897.11 123897.11 to 123 897.11 I have tried Decimal.ToString("0 000.00"). Although this works pretty well when the number is 1897.11. But when it's 897.11 I get 0 897.11. 回答1: Pass in a custom NumberFormatInfo with a custom NumberGroupSeparator property, and use the #,# format to tell it to do number groups. This example

C# Decimal.Parse issue with commas

时间秒杀一切 提交于 2019-12-17 18:18:22
问题 Here's my problem (for en-US): Decimal.Parse("1,2,3,4") returns 1234, instead of throwing an InvalidFormatException. Most Windows applications (Excel en-US) do not drop the thousand separators and do not consider that value a decimal number. The same issue happens for other languages (although with different characters). Are there any other decimal parsing libraries out there that solve this issue? Thanks! 回答1: It's allowing thousands, because the default NumberStyles value used by Decimal

Android - Round to 2 decimal places [duplicate]

孤人 提交于 2019-12-17 17:27:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Round a double to 2 significant figures after decimal point I know that there are plenty of examples on how to round this kind numbers. But could someone show me how to round double, to get value that I can display as a String and ALWAYS have 2 decimal places? 回答1: You can use String.format("%.2f", d) , your double will be rounded automatically. 回答2: One easy way to do it: Double d; Int i; D+=0.005; i=d*100;

Objective C Issue With Rounding Float

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 16:39:42
问题 I have an issue with rounding the result of a calculation to two decimal places. It is a financial calculation and when the result involves half a penny I would expect the number to be rounded up but it is in fact being rounded down. To replicate the issue: float raw = 16.695; NSLog(@"All DP: %f",raw); NSLog(@"2 DP: %.2f",raw); Returns: All DP: 16.695000 2 DP: 16.69 Whereas I would expect to see: All DP: 16.695000 2 DP: 16.70 Can anyone advise if this is by design or if (most likely) I am

Parse decimal and filter extra 0 on the right?

非 Y 不嫁゛ 提交于 2019-12-17 16:09:45
问题 From a XML file I receive decimals on the format: 1.132000 6.000000 Currently I am using Decimal.Parse like this: decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture); How do print myDecimal to string to look like below ? 1.132 6 回答1: I don't think there are any standard numeric format strings which will always omit trailing insignificant zeroes, I'm afraid. You could try to write your own decimal normalization method, but

Remove 0s from the end of a decimal value [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-17 16:07:37
问题 This question already has answers here : Remove trailing zeros (17 answers) Closed 5 years ago . I have a decimal value that has a variable number of digits after the . , for example: 0.0030 0.0310 0.0001 1.1200 How can I write a dynamic function that removes 0 in the end of the decimal? 回答1: string.Format("{0:0.#####}", 0.0030) or var money=1.3000m; money.ToString("0.#####"); For future reference I recommend the .NET Format String Quick Reference by John Sheehan. 回答2: You can also modify the