Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing zero on decimal

Tags:

asp.net

vb.net

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

like image 355
Jamie Taylor Avatar asked Nov 16 '10 15:11

Jamie Taylor


People also ask

Does 0.5 have a trailing zero?

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.

What is the rule for trailing zeros?

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.

How do you get rid of trailing zero in decimals?

You can remove trailing zeros using TRIM() function.

What is meant by trailing zero?

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.


2 Answers

float.Parse("512.3").ToString("0.00");

It would give the number with two decimal digits.

like image 170
Shadow Wizard Hates Omicron Avatar answered Oct 19 '22 13:10

Shadow Wizard Hates Omicron


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.

like image 22
jcolebrand Avatar answered Oct 19 '22 15:10

jcolebrand