Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel number format to only show decimals when necessary

How can I, without using formulas, show integers as integers, but decimals limited to a specific number of decimal places?

E.g. show:
1 as 1
12 as 12
but 1.23456789 as 1.23

The number format 0.## is close but it shows 1 as 1. etc.

like image 375
TDP Avatar asked Apr 24 '18 17:04

TDP


People also ask

What are the steps to format a cell to the required number of decimal places?

Answer. On the File tab, click Options. Click Advanced, and then under Editing options, select the Automatically insert a decimal point check box. In the Places box, enter a positive number four digits to the right of the decimal point or a negative number for digits to the left of the decimal point.


4 Answers

If acceptable, you may use the formula not in the sheet but as a condition for Conditional Formatting.

When your data is at A1:A4 as the sample on screenshot, we should create 2 Formatting Rules:

  1. Formula is: =MOD($A1,1) = 0 Applies to: =$A$1:$A$4 Format the number as no decimals from the format of this rule.
  2. Formula is: =MOD($A1,1) <> 0 Applies to =$A$1:$A$4 & Format the number to show 2 decimals.

Actually the 2nd condition is optional. You may format the whole column with 2 decimals and conditional format with the first rule so only the integers will have no decimals.

Please see my sample screenshot:

enter image description here

like image 152
Hakan ERDOGAN Avatar answered Oct 21 '22 21:10

Hakan ERDOGAN


=IF(MOD(A1,1)=0,TEXT(A1,"0"),TEXT(A1,"0.00"))
like image 42
Juste Avatar answered Oct 21 '22 21:10

Juste


A bit late to the Thread but I have a report which was calculating percentages to 7 decimal places, but the column also contains integers.

My conclusion was to use conditional formatting so if the Cell Contains a period/decimal point "." then to change the formatting to Number with 2 decimal places. This way if there isn't a period/decimal point then the values remain unedited.

Click Here for Full Image

like image 3
Adam Avatar answered Oct 21 '22 21:10

Adam


If you would like to use the value as text you can use this (using A1 as the number):

=TEXT(A1,IF(MOD(A1,1)=0,"#","#.##"))


Explanation:
IF(MOD(A1,1)=0: This part checks if the number is a whole number with the modulo function.

"#","#.##": Use "#" if the condition is true and "#.##" if the condition is false. These are appropriate format options for integer and decimal numbers accordingly.

The number of hashes after the decimal in "#.##" define the maximum precision, but it will only display as many as required e.g. 2.1 would not be "2.10", but "2.1" instead.

Alternatively: "#.00" can be used to always pad with 0's, which would make "1.3" become "1.30".

like image 1
Christoff Avatar answered Oct 21 '22 23:10

Christoff