Wednesday, 15 February 2012

c# - Retain decimal places in toString -



c# - Retain decimal places in toString -

is there way create resulting string 'double.tostring()' contain exact value of double?

for example

double d = 123.0 -> tostring() "123"

i've found little bit of solution in using custom format .tostring("#.0") if want add together more decimal places i'm started

double d = 123.00 -> tostring("#.0") 123.0 double d = 123.0 -> tostring("#.00") 123.00

the reason i'm trying because i've got custom text boxes must utilize different value 1 thats displayed , when trying come in own value text box textbox ignoring text when seek come in 0 in decimal places. hence need tostring value equal exact value of double number can entered properly.

thank you

is there way create resulting string 'double.tostring()' contain exact value of double?

it does contain exact value of double - double doesn't store difference between 123, , 123.0 , 123.00.

decimal stores difference, - , far more appropriate if care exact decimal digits anyway.

on other hand, sounds actually you're doing comparing in wrong place:

the reason i'm trying because i've got custom text boxes must utilize different value 1 thats displayed , when trying come in own value text box textbox ignoring text when seek come in 0 in decimal places.

don't compare string values - parse value (to either decimal or double) , compare parsed values.

this prevent problems leading zeroes, "0123" , "123" represent same value, different strings.

c# winforms textbox rounding tostring

No comments:

Post a Comment