Is there a way to format number with variable decimal places?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,537
Office Version
  1. 365
Platform
  1. Windows
I have a column of numbers that range from the thousandths (.nnn) to the thousands n,nnn. I'd like to format them with a variable number of decimal places according to this table:

ValueDecimal places
<13
<102
<1001
>=1000

<tbody>
</tbody>

I only want to vary the appearance of the value, not the value itself.

I know I can use a nested IF statement with the Format function to get the desired formatting, but this changes the value in the cell. I can do the same thing with a UDF.

I know I can create a second column for the formatted value so the original value is preserved in the other column.

Is there another way so I can use a single column, vary the formatting, and not change the values?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
LOL! You need better reasons to shy away from c.f. than these. But it's your work. So, if you find the alternatives proposed in this discussion easier to use and understand and "more efficient" then you should continue to stay away from c.f.s
I've fiddled around with CF a few times, but always found it awkward for what I was trying to do. I've also read that it can affect performance if applied over a range.
 
Upvote 0
I've fiddled around with CF a few times, but always found it awkward for what I was trying to do.

Me, too; at least, the CF features introduced in XL2007.

I eschew those features by simply clicking on CF, New Rule, "Use a formula".

Then we're back to the simple pre-XL2007 interface: enter a formula which applies the format when true; and click on Format.

Tusharm makes a good point about selecting Stop If True. I believe we must click on Manage Rules to do that.

Regarding suggestions that rely on TEXT:
these return a text value. I would really prefer to only change the formatting, not the value itself.

I think you were very clear about that requirement at the outset.
 
Upvote 0
Excel Custom Number Formats will only take 2 conditions, rather than your requested 3.
Would some variation of 3 cases, like
[<1]#.000;[<10]0.00;0.0

work for you

This turned out to be the best solution. Thanks for reminding me.

BTW: It does take 3 conditions if you count the last one, which is "neither of the previous two".
 
Upvote 0
PS: I also wrote a little UDF to convert the number into a test string. It's not what I need for this application, but might be useful in another situation. Here it is in case anyone else might find it useful.

Code:
'======================================================================
'                          Convert Odds Function

' Convert the odds provided as a percentage into a "nnn.nn/1" format.

' Syntax: =CvtOdds(PCOdds)

'   PCOdds  The odds as a percentage
======================================================================
Function CvtOdds(PCOdds As Double) As String

Select Case PCOdds
  Case Is < 1
    CvtOdds = Format(PCOdds, "0.00")
  Case Is < 10
    CvtOdds = Format(PCOdds, "0.0")
  Case Is < 100
    CvtOdds = Format(PCOdds, "0")
  Case Else
    CvtOdds = Format(PCOdds, "#,##0")
End Select

CvtOdds = CvtOdds & "/1"

End Function
 
Upvote 0
PS: I also wrote a little UDF to convert the number into a test string. It's not what I need for this application, but might be useful in another situation. Here it is in case anyone else might find it useful.

Code:
'======================================================================
'                          Convert Odds Function

' Convert the odds provided as a percentage into a "nnn.nn/1" format.

' Syntax: =CvtOdds(PCOdds)

'   PCOdds  The odds as a percentage
======================================================================
Function CvtOdds(PCOdds As Double) As String

Select Case PCOdds
  Case Is < 1
    CvtOdds = Format(PCOdds, "0.00")
  Case Is < 10
    CvtOdds = Format(PCOdds, "0.0")
  Case Is < 100
    CvtOdds = Format(PCOdds, "0")
  Case Else
    CvtOdds = Format(PCOdds, "#,##0")
End Select

CvtOdds = CvtOdds & "/1"

End Function
If I am not mistaken, the following function should return the same values as your function above...
Code:
Function CvtOdds(PCOdds As Double) As String
  CvtOdds = Format(PCOdds, Left("#,##0.00", 8 + (PCOdds >= 1) + 2 * (PCOdds >= 10))) & "/1"
End Function
 
Upvote 0
If I am not mistaken, the following function should return the same values as your function above...
Code:
Function CvtOdds(PCOdds As Double) As String
  CvtOdds = Format(PCOdds, Left("#,##0.00", 8 + (PCOdds >= 1) + 2 * (PCOdds >= 10))) & "/1"
End Function

Wow. Are you a member of the infamous one-liner club? :)

It reminds me of an old IBM programming language called APL (A Programming Language). The code was so dense that they had competitions to see who could write the most complex programs in one line. One guy wrote a complete conversion from Arabic numbers to Roman numerals.

I tested your code and it seems to work. It's not so good for me because it's not immediately obvious to me what it does. If I were to decide to change the thresholds or the formats, it would take me awhile to figure out just what to do and I'd probably make mistakes. My code may not be as elegant, but I can change it in seconds.
 
Upvote 0

Forum statistics

Threads
1,215,700
Messages
6,126,279
Members
449,308
Latest member
VerifiedBleachersAttendee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top