Search code examples
c#type-conversionformatdouble

When converting a string to double the output is whitout leading zeros


I have a textbox where i get my string value from. When using

tbValue = (Convert.ToDouble(tbValue.text) * -1.0).toString();

the output is allways without the leading zeros.

when the value is 0,8 it returns -8 in the textbox.

Is there a way to keep the leading zero when using Convert.ToDouble?


Solution

  • First, your error is not about "leading zeroes". A double is a value, values have no surplus leading zeroes (like in 003.3) - those are added in formatting the output.

    If your input of "0.8" is recognized as "8" that is not a leading zero issue, it is a decimal separator issue - the "ToDouble" part is assuming - likely - that there is a "." as decimal separator, so the "," is ignored. The number is only a proper number if parsed with the correct locale for your system (default is to the system language). You can give an explicit locale in Parsing, check all overloads of ToDouble.