Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double.Parse precision/Rounding issue

I have one column in database with datatype

decimal(24,10)

Let suppose I have value like

string d1 = "123.6666666666";

Double.Parse(d1) 

output in datacolumn : 123.6666666700

I have used Convert.ToDecimal(d1) which gives output 123.6666666666.

Expected output :123.7000000000

like image 431
Mukund Avatar asked Nov 17 '25 11:11

Mukund


1 Answers

The output you got is because the precision of double and decimal values. What you actualy want is to round the number to 1 fractional digit, for that you have to use Math.Round, either with a double or decimal, according to your needs, like this:

Math.Round(Double.Parse(d1),1)

Or, if you need

Math.Round(Convert.ToDecimal(d1),1)

For more information about Math.Round, check the MSDN link: https://msdn.microsoft.com/pt-br/library/75ks3aby(v=vs.110).aspx

like image 90
Magnetron Avatar answered Nov 19 '25 00:11

Magnetron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!