简体   繁体   中英

format a double to 8 decimal places

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

double d = 0.123456789;
string sDisplayValue = d.ToString("0.00000000");
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM