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?
CodePudding user response:
Are you using "," ? It works with "."
string input = "0.8";
string tbValue = (Convert.ToDouble(input) * -1.0).ToString();
https://dotnetfiddle.net/XAsNhe
CodePudding user response:
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.
CodePudding user response:
This work
string tbValue = (Convert.ToDouble(tbValue.Text.Replace(",","."), CultureInfo.InvariantCulture) * -1.0).ToString();