Possible Duplicate:
Display Float as String with at Least 1 Decimal Place
Given the following values:
How can I achieve the following display:
What I am trying to do is force 2 decimal places UNLESS there are more "significant" digits, in which case I'd like to show them as well.
Note: The value is currently stored as a Decimal?
The number of decimal places is the number of digits to the right of the decimal point, while the number of significant digits is the number of all digits ignoring the decimal point, and ignoring all leading zeros and some trailing zeros (for a fuller definition see 'significant figures' on Wikipedia).
For the number of decimal places stated, count that number of digits to the right of the decimal and underline it. The next number to its right is called the 'rounder decider'. If the 'rounder decider' is 5 or more, then round the previous digit up by 1.
4.732 rounded to 2 decimal places would be 4.73 (because it is the nearest number to 2 decimal places). 4.737 rounded to 2 decimal places would be 4.74 (because it would be closer to 4.74). 4.735 is halfway between 4.73 and 4.74, so it is rounded up: 4.735 rounded to 2 decimal places is 4.74.
Using the format() method "%. 2f" denotes 2 decimal places, "%. 3f" denotes 3 decimal places, and so on.
Use
yourNumber.ToString(".00######")
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