How can i truncate the leading digit of double value in C#,I have tried Math.Round(doublevalue,2) but not giving the require result. and i didn\'t find any other method in M
EDIT: It's been pointed out that these approaches round the value instead of truncating. It's hard to genuinely truncate a double
value because it's not really in the right base... but truncating a decimal
value is more feasible.
You should use an appropriate format string, either custom or standard, e.g.
string x = d.ToString("0.00");
or
string x = d.ToString("F2");
It's worth being aware that a double value itself doesn't "know" how many decimal places it has. It's only when you convert it to a string that it really makes sense to do so. Using Math.Round
will get the closest double value to x.xx00000
(if you see what I mean) but it almost certainly won't be the exact value x.xx00000
due to the way binary floating point types work.
If you need this for anything other than string formatting, you should consider using decimal
instead. What does the value actually represent?
I have articles on binary floating point and decimal floating point in .NET which you may find useful.