Excel number format to only show decimals when necessary

后端 未结 6 1180
旧巷少年郎
旧巷少年郎 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:32
    =IF(MOD(A1,1)=0,TEXT(A1,"0"),TEXT(A1,"0.00"))
    
    0 讨论(0)
  • 2021-01-12 00:40

    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

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2021-01-12 00:44

    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:

    0 讨论(0)
  • 2021-01-12 00:48

    Maybe an example used to put coordinates to a point could help.

    Sub PutCoord(PtN&, Px#, Py#, S4$)
    
    Px = Round(Px, 2): Py = Round(Py, 2)
    
     Dim XS$: If Px = Int(Px) Then XS = Format(Px, "0") Else XS = Format(Px, "0.##")
    
     Dim YS$: If Py = Int(Py) Then YS = Format(Py, "0") Else YS = Format(Py, "0.##")
    
       Dim WS$: WS = "Pt " & PtN & " @ " & XS & " , " & YS
    
       With ActiveSheet.Shapes.AddShape(msoShapeDoubleBracket, Px, Py, 90, 20)
    
          With .TextFrame
    
             .MarginLeft = 0
    
             .MarginRight = 0
    
             .MarginTop = 0
    
             .MarginBottom = 0
    
             .Characters.Text = WS
    
             .AutoSize = msoAutoSizeShapeToFitText
    
          End With
    
          .Name = S4 & PtN
    
       End With
    
    End Sub
    
    0 讨论(0)
  • 2021-01-12 00:52

    Us if and text formula to format your values. If your values are in column A, use this formula:

    =IF(LEN(A1)=1,TEXT(A1,"0"),TEXT(A1,"0.00"))
    

    This formula basically says if length of cell value is 1, then return formatted value as "0", otherwise return value as "0.00".

    0 讨论(0)
提交回复
热议问题