I want to convert this string: 0.55000000000000004
to this double: 0.55
.
How to do that?
Asked
Active
Viewed 5.6k times
3 Answers
52
you can use this code to reduce precision part:
double m = Math.Round(0.55000000000000004,2);
Result would be : 0.55

mjyazdani
- 2,110
- 6
- 33
- 64
32
Is a string or a double? If it is a string:
double d = double.Parse(s,CultureInfo.InvariantCulture);
string s=string.Format("{0:0.00}",d);
if it is already a double just format using the second line.

Felice Pollano
- 32,832
- 9
- 75
- 115
-
The CultureInfo.InvariantCulture format provider is the one to use if your decimal separator is always ".", what exactly you need to know ? – Felice Pollano Feb 14 '11 at 21:03
-
1And if you want to do it without trailing zeroes you can use [{0:#,0.######}](http://stackoverflow.com/a/20146494/492336) – sashoalm Jun 20 '16 at 08:49
12
There is no double 0.55 - the number cannot be accurately represented as a binary fraction. Which is probably the reason why you got that long string in the first place. You should probably be using the decimal
type instead of double
.
Read The Floating-Point Guide to understand why.

Michael Borgwardt
- 342,105
- 78
- 482
- 720
-
5@PokemonCraft: No. What you need is to understand why what you want is impossible and does not make sense. – Michael Borgwardt Feb 14 '11 at 21:11