Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show 2 decimals places (unless there are more significant digits) [duplicate]

Possible Duplicate:
Display Float as String with at Least 1 Decimal Place

Given the following values:

  • 23.50
  • 15
  • 19.3500
  • 31.505
  • 45.6670

How can I achieve the following display:

  • 23.50
  • 15.00
  • 19.35
  • 31.505
  • 45.667

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?

like image 286
Brian David Berman Avatar asked Dec 26 '12 16:12

Brian David Berman


People also ask

What is the difference between decimal place and significant figures?

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).

How to decide how many decimal places to use?

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.

What is a number with 2 decimal places?

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.

How do I fix decimal places in Java?

Using the format() method "%. 2f" denotes 2 decimal places, "%. 3f" denotes 3 decimal places, and so on.


1 Answers

Use

yourNumber.ToString(".00######")
like image 156
lante Avatar answered Oct 12 '22 13:10

lante