Solana token decimals: convert raw supply without losing a digit
By MassLauncher · Updated · 2 min read
With six decimals, a raw supply of 1234567 is 1.234567 tokens. Keep the raw digits as a string: rounding even once can change the reported supply.
Read amount and decimals together
Call getTokenSupply with a mint address. In value, amount is a string of integer base units and decimals tells you where the decimal point goes for an ordinary mint. The result describes the mint’s current total supply. It cannot tell you how much a particular holder owns or how much supply is circulating. Keep the response context slot when comparing supply at different times.
| Raw amount (string) | Decimals | Token units |
|---|---|---|
| 1 | 0 | 1 |
| 1 | 6 | 0.000001 |
| 1234567 | 6 | 1.234567 |
| 1234567 | 9 | 0.001234567 |
| 9007199254740993 | 6 | 9007199254.740993 |
Format the digits without a float
If decimals is zero, return the raw string. Otherwise pad on the left with zeroes until you have at least decimals + 1 digits, then insert a point decimals places from the right. For a raw amount of 1 with six decimals, the padded string is 0000001. Splitting after the first digit gives 0.000001.
The last raw amount in the table exceeds the safe integer range of a JavaScript Number. Converting it to Number before formatting can lose the final unit. Preserve amount as text in spreadsheets and CSV imports too; an import that guesses a numeric column type can round long values before you see them.
When a wallet uses a different display
Token-2022’s scaled UI amount extension can apply a multiplier to the displayed amount without changing the raw supply. Interest-bearing mints can also have a time-dependent display. For those mints, first identify the enabled extension and use its conversion rules if you need to reproduce the wallet view. Keep the raw amount, decimals and observation time with the result.