format a double to 8 decimal places

2019-08-04 02:34发布

问题:

I have a method that i want to return as a double with 8 dp.

so i have a value coming back from a sp and in the code i am simply doing

CheckSum = double.Parse(cmd.ExecuteScalar().ToString());

however, if the sp returns this :1541338.2349537 the returned double only has those 7dp, when i would like it to put a trailing zero on. like this.. 1541338.23495370

i cant find a method on the double to format it - ie force it to 8dp, other than to return a formatted string. which i dont want to do. can someone explain please :)

thanks

nat

回答1:

double d = 0.123456789;
string sDisplayValue = d.ToString("0.00000000");


回答2:

double inputValue = 14.42564678942; inputValue = Math.Round(inputValue, 8);

output is 14.42564679

This will preserve the double and will not need a conversion to string



标签: c# format double