How to format very large numbers?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,643
Office Version
  1. 365
Platform
  1. Windows
I need to format numbers from 0 to 10^24 or more. Here's what I'd like

0 - 999 -> as is
1,000 - 999,000 -> xxx thousand
1,000,000 - 999,000,000 -> xxx million
1,000,000,000 - 999,000,000,000 -> xxx billion
... and so on

Is there a formatting string that will do that?

Is there a built-in function that will convert the number into that string or does anyone have a UDF that will do that.

I don't care whether the result is a number or a string.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I made this, it would work all the way up to e24 and beyond, if it wasn't for Excel starting to convert numbers to scientific notation after 100 Trillion. At that point the function starts outputting "1E Thousand", incorrectly. I might revisit to fix it, or maybe someone else can look at it.

VBA Code:
Function namesOfLargeNumbers(n As Double)
On Error GoTo ErrorHandler
    If Not IsNumeric(n) Then GoTo ErrorHandler
    Dim nStr As String
    
    nStr = CStr(n)
    l = Len(nStr)
    
    If l < 4 Then namesOfLargeNumbers = n: GoTo Continue
    
    Dim d
    Set d = CreateObject("Scripting.Dictionary")
    d.Add 4, "Thousand"
    d.Add 7, "Million"
    d.Add 10, "Billion"
    d.Add 13, "Trillion"
    d.Add 16, "Quadrillion"
    d.Add 19, "Quintillion"
    d.Add 22, "Sextillion"
    d.Add 25, "Septillion"
    
    Dim name As String
    Dim k As Integer
    For i = 0 To d.Count - 1
        If l >= d.Keys()(i) And l < d.Keys()(i + 1) Then
            name = d.Items()(i)
            k = d.Keys()(i) - 1
            Exit For
        End If
    Next
    
    remainingDigits = Left(n, l - k)
    'Debug.Print remainingDigits, name
    
    namesOfLargeNumbers = remainingDigits & " " & name

Continue:
Exit Function

ErrorHandler:
namesOfLargeNumbers = "<Error>"
Exit Function

End Function
 
Upvote 0
I got this code to work all the way up to decillions!

VBA Code:
Function ConvertToScaledText(pNumber)

Application.Volatile

Debug.Assert False
Stop

Dim aUnits() As String          'Array for units
Const sUnits As String = "x thousand million billion trillion quadrillion" _
                         & " quintillion sextillion septillion octillion nonillion" _
                         & " decillion"
aUnits = Split(sUnits, " ")     'Split the units into the array
aUnits(LBound(aUnits)) = ""     'Make <1000 null

Dim nScaled As Double   'Number that gets scaled along the way
nScaled = pNumber       '.Start with input number

Dim i As Long           'Loop index
For i = LBound(aUnits) To UBound(aUnits)    'Go through the units
                                'If the scaled number < 100 OR this is the last unit
    If Round(nScaled, 0) < 1000 Or i = UBound(aUnits) Then
        ConvertToScaledText = Format(Round(nScaled, 0), "#,##0") & " " & aUnits(i)
        Exit Function               'Do the formatting & exit
    Else                        'If scaled number is still > 1000
        nScaled = nScaled / 1000    'Scale another 3 decimal places & try again
    End If
Next i

End Function

Here's a minisheet testing it. Let me know if you see any errors or know a better way.

ConvertToScaledText.xlsx
CD
4NumberConvertToScaledText
50.49990
60.50000
70.51001
8999.4990999
9999.50001 thousand
10999,499.0000999 thousand
11999,500.00001 million
12999,499,000.0000999 million
13999,500,000.00001 billion
14999,499,000,000.0000999 billion
15999,500,000,000.00001 trillion
16999,499,000,000,000.0000999 trillion
17999,500,000,000,000.00001 quadrillion
18999,499,000,000,000,000.0000999 quadrillion
19999,500,000,000,000,000.00001 quintillion
20999,499,000,000,000,000,000.0000999 quintillion
21999,500,000,000,000,000,000.00001 sextillion
22999,499,000,000,000,000,000,000.0000999 sextillion
23999,500,000,000,000,000,000,000.00001 septillion
24999,499,000,000,000,000,000,000,000.0000999 septillion
25999,500,000,000,000,000,000,000,000.00001 octillion
26999,499,000,000,000,000,000,000,000,000.0000999 octillion
27999,500,000,000,000,000,000,000,000,000.00001 nonillion
28999,499,000,000,000,000,000,000,000,000,000.0000999 nonillion
29999,500,000,000,000,000,000,000,000,000,000.00001 decillion
309,994,990,000,000,000,000,000,000,000,000,000,000.00009,995 decillion
31999,499,000,000,000,000,000,000,000,000,000,000,000.0000999,499 decillion
3299,949,900,000,000,000,000,000,000,000,000,000,000,000.000099,949,900 decillion
33900,000,000,000,000,000,000,000,000,000,000,000,000,000,000.0000900,000,000,000 decillion
Sheet1
Cell Formulas
RangeFormula
D5:D33D5=ConvertToScaledText([@Number])
 
Upvote 0
If it works, it's good :) Curious though, if you Debug.Print pNumber at the top of your function, what does it print for 1 quadrillion or larger?
 
Upvote 0
If it works, it's good :) Curious though, if you Debug.Print pNumber at the top of your function, what does it print for 1 quadrillion or larger?

Interesting. 🤔🤨🤔🤨 That took a bit of work. 😣😣 Just adding that statement was not very helpful. I got 29 results in the Immediate window, but they were not in the same order as the table. 😣 So I added a Row column to the table and passed that to the UDF. It then printed the row number with the value of pNumber. I was then able to copy those values into Word, sort o the row, and paste that back into Excel. The results are in this minisheet. Do you see a problem?

ConvertToScaledText.xlsx
CDEF
4NumberRowConvertToScaledTextpNumber
50.4999100.4999
60.5000200.5
70.5100310.51
8999.49904999999.499
9999.500051 thousand999.5
10999,499.00006999 thousand999499
11999,500.000071 million999500
12999,499,000.00008999 million999499000
13999,500,000.000091 billion999500000
14999,499,000,000.000010999 billion9.99499E+11
15999,500,000,000.0000111 trillion9.995E+11
16999,499,000,000,000.000012999 trillion9.99499E+14
17999,500,000,000,000.0000131 quadrillion9.995E+14
18999,499,000,000,000,000.000014999 quadrillion9.99E+17
19999,500,000,000,000,000.0000151 quintillion1.00E+18
20999,499,000,000,000,000,000.000016999 quintillion9.99E+20
21999,500,000,000,000,000,000.0000171 sextillion1.00E+21
22999,499,000,000,000,000,000,000.000018999 sextillion9.99E+23
23999,500,000,000,000,000,000,000.0000191 septillion1.00E+24
24999,499,000,000,000,000,000,000,000.000020999 septillion9.99E+26
25999,500,000,000,000,000,000,000,000.0000211 octillion1.00E+27
26999,499,000,000,000,000,000,000,000,000.000022999 octillion9.99E+29
27999,500,000,000,000,000,000,000,000,000.0000231 nonillion1.00E+30
28999,499,000,000,000,000,000,000,000,000,000.000024999 nonillion9.99E+32
29999,500,000,000,000,000,000,000,000,000,000.0000251 decillion1.00E+33
309,994,990,000,000,000,000,000,000,000,000,000,000.0000269,995 decillion9.99E+36
31999,499,000,000,000,000,000,000,000,000,000,000,000.000027999,499 decillion9.99E+38
3299,949,900,000,000,000,000,000,000,000,000,000,000,000.00002899,949,900 decillion9.99E+40
33900,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000029900,000,000,000 decillion9.00E+44
Sheet1
Cell Formulas
RangeFormula
D5:D33D5=ROW()-ROW(Table1[[#Headers],[Row]])
E5:E33E5=ConvertToScaledText([@Number],[@Row])


PS: I pasted the values into an Excel column formatted as Text, so Excel wouldn't mess with them. 👍
 
Upvote 0
No no, your function seems to work fine, I was only asking to figure out why my approach isn't workng :D
 
Upvote 0
No no, your function seems to work fine, I was only asking to figure out why my approach isn't workng :D

Ok. Let me know if you need anything more. Of course you have my code, so you can test it yourself.

I really like this function. I needed it because I was working on a sheet that would compare relativistic travel times to various destinations in the universe. The universe is huge. The distances in light years vary from 5.92E-06 (35 million miles to Mars) to 4.60E+10 (270 sextillion miles to the edge of the universe).

I feel like it should be built into Excel as a formatting option. It already lets us scale by affixing commas (#,##0,,). But of course when you start dealing units, especially variable units, then you get into metric vs US vs British. 😣😢😒
 
Upvote 0
You could use this to build a formula like this, (so you have an alternative to a UDF):

Formato.xlsx
EF
511
61010
7100100
810001 Thousand
91000010 Thousand
10100000100 Thousand
1110000001 Million
121000000010 Million
13100000000100 Million
1410000000001 Billion
151000000000010 Billion
16100000000000100 Billion
1710000000000001 Trillion
181000000000000010 Trillion
19100000000000000100 Trillion
2010000000000000001 Quadrillion
211000000000000000010 Quadrillion
22100000000000000000100 Quadrillion
2310000000000000000001 Quintillion
241000000000000000000010 Quintillion
25100000000000000000000100 Quintillion
2610000000000000000000001 Sextillion
271000000000000000000000010 Sextillion
Hoja1 (2)
Cell Formulas
RangeFormula
F5:F27F5=LET(t,TEXT(E5,"##0E+0"),TEXTBEFORE(t,"E")&" "&XLOOKUP(TEXTAFTER(t,"E"),{"+0";"+3";"+6";"+9";"+12";"+15";"+18";"+21";"+24"},{"";"Thousand";"Million";"Billion";"Trillion";"Quadrillion";"Quintillion";"Sextillion";"Septillion"},"not in list",0))


You can update the arrays to your needs. Or use a helper table in a hidden sheet for example with the {"+0";"+3";"+6";"+9";"+12";"+15";"+18";"+21";"+24"},{"";"Thousand";"Million";"Billion";"Trillion";"Quadrillion";"Quintillion";"Sextillion";"Septillion"} values
 
Upvote 0
A formatting string that does something similar, but not quite as you asked. It is like notation used in engineering:

That's a nice option to have. Thanks for reminding me about using # for scaling in scientific notation. Very helpful.

I may also look into doing the formatting in my UDF and then converting the exponent to the text number (+3 -> thousand, +6 -> million, etc.).
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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