I have a string which looks like this 512.3
is there a way to add a trailing zero so it looks like this 512.30
Just to clarify (sorry I didn't know there where different ways etc.)
My string is an amount which is passed so changes all the time I only need the trailing zero on amounts like 512.3
, 512.4
,512.5
etc. as some of my amounts will pass values like 512.33
and 512.44
and so on
Thanks
Jamie
If trailing zeros are placed after the decimal point, they carry the weight of significant figures. Thus, the number 0.50 means 0.5 � 0.01.
To determine the number of significant figures in a number use the following 3 rules: Non-zero digits are always significant. Any zeros between two significant digits are significant. A final zero or trailing zeros in the decimal portion ONLY are significant.
You can remove trailing zeros using TRIM() function.
A trailing zero is a zero digit in the representation of a number which has no non-zero digits that are less significant than the zero digit. Put more simply, it is a zero digit with no non-zero digits to the right of it.
float.Parse("512.3").ToString("0.00");
It would give the number with two decimal digits.
You're going to want to use String.Format or some derivation thereof, and the format string will look like
myString = String.Format("{0:F2}",myObject);
Also note that format strings can be used in the .ToString("F2")
method (notice I included the format string F2
inside there already.
See the MSDN links above for a more thorough and definitive explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With