Excel number format to only show decimals when necessary

后端 未结 6 1219
旧巷少年郎
旧巷少年郎 2021-01-12 00:32

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

6条回答
  •  死守一世寂寞
    2021-01-12 00:40

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

提交回复
热议问题