---
id: HS0131
title: "Raw to UI amount conversion"
url: https://carbium.sh/library/howsolana/raw-to-ui-amount-conversion
collection: How to use Solana
category: SPL tokens
revision: 1
updated: 2026-11-23
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Raw to UI amount conversion

A raw token amount is an integer counting the smallest units of a mint. One TypeScript file reads the mint's decimals from Carbium RPC and converts the raw amount to exact decimal text with BigInt, so 1000000 prints as 1 for USDC.

## What a raw amount is

On Solana a token amount is stored as one integer. The integer counts the smallest units of the token, and the mint sets how many of those units make one whole token. That count is `decimals`, and it lives on the mint account, not on the token account that holds the balance. USDC has 6 decimals on mainnet, so the integer 1000000 is 1 USDC, and 1500000 is 1.5 USDC.

The conversion moves the decimal point, and it stays exact only while the arithmetic runs on integers. Divide the raw integer by 10 to the power of `decimals` to get the whole part, and the remainder gives the fraction. `BigInt` keeps every digit. A JavaScript number holds integers exactly only up to 2^53, and a token with 9 decimals and a supply of one billion tokens sits well past that line.

| Raw amount | Decimals | UI amount |
|---|---|---|
| 1000000 | 6 | 1 |
| 1500000 | 6 | 1.5 |
| 1 | 9 | 0.000000001 |
| 1000000000000000000 | 9 | 1000000000 |

The file below takes the raw amount from the command line and the mint from a second argument, which defaults to USDC. It gets `decimals` from the chain with `getTokenSupply`, because that call returns the decimals of the mint next to its total supply. The total supply goes through the same conversion, so the output can be checked against the amount the node formats.

## The code

Install `@solana/kit` at 8.6.0, export `CARBIUM_KEY` and run the file with Node 24, which strips the types on load. The transport sends every call to `https://services.carbium.sh` with the key in the `x-api-key` header. The request asks for `confirmed` commitment. The conversion itself makes no network call and uses only the decimals the chain returned.

`main.ts` (run: `node main.ts <raw-amount> [mint-address]`)

```ts
// HS0131 · Raw to UI amount conversion
// https://carbium.sh/library/howsolana/raw-to-ui-amount-conversion
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

import { address, createDefaultRpcTransport, createSolanaRpcFromTransport } from "@solana/kit";

const key = process.env.CARBIUM_KEY;
if (!key) {
  console.error("Set CARBIUM_KEY to your Carbium API key.");
  process.exit(1);
}
const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";
const rpc = createSolanaRpcFromTransport(
  createDefaultRpcTransport({ url, headers: { "x-api-key": key } }),
);

const USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
const [rawArg, mintArg = USDC] = process.argv.slice(2);
if (!rawArg || !/^\d+$/.test(rawArg)) {
  console.error("Usage: node main.ts <raw-amount> [mint-address]");
  process.exit(1);
}
const raw = BigInt(rawArg);
const mint = address(mintArg);

// Whole part and fraction from integers only: no float touches the digits.
function toUi(amount: bigint, decimals: number): string {
  if (decimals === 0) return amount.toString();
  const scale = BigInt(10) ** BigInt(decimals);
  const fraction = (amount % scale).toString().padStart(decimals, "0").replace(/0+$/, "");
  const whole = (amount / scale).toString();
  return fraction === "" ? whole : `${whole}.${fraction}`;
}

const { value: supply } = await rpc
  .getTokenSupply(mint, { commitment: "confirmed" })
  .send();

console.log(`mint ${mint}`);
console.log(`decimals ${supply.decimals}`);
console.log(`${raw} raw = ${toUi(raw, supply.decimals)} UI`);
console.log(`total supply ${supply.amount} raw = ${toUi(BigInt(supply.amount), supply.decimals)} UI`);
console.log(`node uiAmountString ${supply.uiAmountString}`);
```

The function `toUi` works on integers alone. It splits the amount into a whole part and a fraction, pads the fraction to `decimals` digits, and drops trailing zeros. A mint with 0 decimals returns the integer as it is. Nothing in the conversion passes through a float, so the digits you read are the digits the chain stores.

## Reading the output

The third line is the conversion you asked for. The fourth converts the total supply through the same function, and the fifth prints the text the node formats for the same number. Compare the fourth line with the fifth by value. The node's text can carry zeros that `toUi` trims, so two strings that differ only in trailing zeros describe the same amount.

> Use the decimals from the same response as the amount. Converting a raw amount of one mint with the decimals of another mint gives a wrong answer that still looks plausible, so the run prints both values next to each other.

To go the other way, from a typed amount such as `1.5` to a raw integer, split the text at the decimal point, pad the fraction to `decimals` digits and join the two parts as `BigInt`. Reject any input with more fraction digits than the mint allows. Cutting the extra digits would send a smaller amount than the one typed, and nothing would show the difference.

## On Carbium

`getTokenSupply` is a read of the mint account, and it uses the same key and endpoint as every other RPC call on Carbium. A call over its rate waits once and is served if its cells refill within one second. Past that it gets a 429 with a retry-after hint, and the file stops on it, so wait for the stated seconds before running it again. The rate table on the [limits](https://carbium.sh/docs/limits) page gives the per-method cost for each plan.

The key goes in the `x-api-key` header as shown in the [auth](https://carbium.sh/docs/auth) reference. The URL comes from `CARBIUM_RPC_URL` when you set it, and falls back to the Carbium endpoint otherwise. Errors come back as JSON-RPC codes, and a method name that Carbium does not serve answers -32601, so check the spelling of the method before anything else. The [errors](https://carbium.sh/docs/errors) page lists the codes.

## Where it ends

The conversion checks the shape of the input and nothing else. A raw amount that is a valid integer but larger than the mint's supply converts without complaint, so compare it with the supply yourself when that matters. The file rejects text that is not a whole number, and it does not check a range.

The mint's total supply is the amount that exists, not the amount that circulates and not the amount a wallet holds. A wallet balance lives in a token account, and the conversion needs that account's raw amount as its input. To read one wallet's balance for a mint, use [SPL token balance](https://carbium.sh/library/howsolana/spl-token-balance), then pass its `amount` through this conversion.

Decimals belong to each mint. A token made with 9 decimals needs no code change, but a hardcoded 6 breaks it silently. Token-2022 mints expose `decimals` through the same call, so the file works for both programs. The file reads one mint per run, and it converts one amount per run.

Do not compute a UI amount with `uiAmount`, the float the node also returns. It has the same 2^53 limit as any JavaScript number, and summing floats drifts from the sum on chain. Keep amounts as `BigInt` while you add or compare them, and convert to text only for display.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| The output shows a raw amount with the same number as the UI amount, such as `1000000 raw = 1000000 UI`. | The decimals came out as 0, or the mint is not the one you meant. Check the `decimals` line the run prints and confirm the mint address, then run again. |
| The call fails for a mint address, with an error that the account is not a token mint. | The address is a wallet or another account. Pass the mint address itself. A token account address is not a mint address. |
| HTTP 429 with a retry-after hint. | Wait for the number of seconds in the header and run the file again. A repeated 429 means the loop around it is too fast for your plan. |
| Error -32601 method not found. | Fix the method name. Carbium answers -32601 for a method it does not serve, and a retry returns the same answer, so do not retry it. |
| Sums of several converted amounts differ from the chain in the last digits. | The sum was taken on floats. Add the raw amounts as `BigInt` values and convert the total once at the end. |

## Questions

### Is 1000000 raw equal to one USDC?

Yes, because USDC mainnet has 6 decimals. Divide the raw integer by 10 to the power of 6 and you get 1. The run prints this as `1000000 raw = 1 UI` for the default mint.

### Why not divide by 1000000 in JavaScript?

Division of a plain number is exact only while the raw integer stays below 2^53. Larger amounts lose their last digits without a warning. `BigInt` keeps every digit, so the conversion is exact for any amount the chain can hold.

### Does the conversion need a Carbium key?

The conversion itself needs no key and no network call. Reading the decimals from the chain does, because the call goes to the RPC endpoint with your key in the `x-api-key` header.

### How do I convert an amount a user types?

Split the text at the decimal point, pad the fraction to the mint's decimals and join the parts as `BigInt`. Reject any input with more fraction digits than the mint allows, so no digit is cut silently.

## Related

- [SPL token balance](https://carbium.sh/library/howsolana/spl-token-balance) (How to use Solana, HS0001)
- [Token prices and metrics](https://carbium.sh/library/usecase/token-prices-and-metrics) (Use cases, UC0008)
- [carbium.sh/docs/rpc](https://carbium.sh/docs/rpc)
- [carbium.sh/docs/auth](https://carbium.sh/docs/auth)
- [carbium.sh/docs/limits](https://carbium.sh/docs/limits)
- [carbium.sh/docs/errors](https://carbium.sh/docs/errors)
- [carbium.sh/docs/endpoints](https://carbium.sh/docs/endpoints)
