c# - Convert string to double with 2 digit after decimal separator -


All codes started with these simple lines:

  string S = "16.9" ; Double D = Convert. Repeat (two); D * = 100;  

The result should be 1690.0, but it is not. D is equal to 1689.9999999999998 What I want to do is to score the double value with two digits after the decimal separator. This is my job.

  Private double round float (double value) {float sign = (value <0)? -0.01f: 0.01f; If (Math.Abus (value) & lt; 0.00001) value = 0; String SVal = Value.ToString (); String decimal separator = System.Globalization.CultureInFinance.Content Culture.numberformet.CurrencyDecallShippire; Int I = SVal.IndexOf (Decimals); If (i> gt; 0) {int SRnd; Try {// Viratize цифра след десетичния разделител SRnd = Convert.ToInt32 (SVL.sstring (i + 3, 1)); } Hold {SRnd = 0; } If (SVL.Land> I + 3) SVL = SVL Asbestring (0, i + 3); // SVL + = "00001"; Try {Dual result = (SRnd> = 5)? Convert Repeat (SVL) + Sign: Convert. Tud Double (Sevill); // result = math.road (result, 2); Return result; } Hold {return 0; }} And {return value; }  

But again there is one problem, changing the string to duplicate I want to do that is not working. A solution to this problem is to add "00001" to the string and then use the monastery. Round function (commented in the example above).

This double value multiplies 100 (in the form of an integer) a device (cash register) and this value should be correct.

I am using VS2005 + .NET CF 2.0

Is there any other "elegant" solution, I am not happy with it. It is one.

Couple can not really represent 16.9. I recommend that you change it to decimal :

  string s = "16.9"; Decimal m = decimal Purse (s) * 100; Double D = (double) m;  

You want to use decimal instead of double , because you say that you will use it for monetary purposes . Remember that the purpose of decimal is to represent the exact decimal digits that are fitted in its precision, whereas double represents only those binary numbers which do this.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -