user15339638
user15339638

Reputation:

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?

Upvotes: 0

Views: 1228

Answers (3)

Francesco Franco
Francesco Franco

Reputation: 81

This work

string tbValue = (Convert.ToDouble(tbValue.Text.Replace(",","."), CultureInfo.InvariantCulture) * -1.0).ToString();

Upvotes: 1

TomTom
TomTom

Reputation: 62159

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.

Upvotes: 2

Leandro Bardelli
Leandro Bardelli

Reputation: 11608

Are you using "," ? It works with "."

    string input = "0.8";
    string tbValue = (Convert.ToDouble(input) * -1.0).ToString();

https://dotnetfiddle.net/XAsNhe

Upvotes: 1

Related Questions