Around Go: blockchain infrastructure · 3 MIN READ

Token amounts: parse exact base units without float64

Convert validated decimal text to bounded integer base units with an explicit scale.

Token amounts frequently exceed the exact integer range of float64, even when the human-readable value looks small. Converting through a float before math/big permanently loses information. Parse text under an explicit decimal-scale policy, reject unsupported precision and preserve the integer base-unit value. Token metadata is input to validate, not an instruction to allocate unbounded memory.

blockchain · A MAP OF THE IDEA

  1. Verify token + scale
  2. Bound decimal input
  3. Parse integer units
  4. Test precision boundaries

Useful when

  • Convert validated decimal text to bounded integer base units with an explicit scale.
  • Makes the owner and the failure boundary explicit.

Watch out

  • Symbols are not unique token identifiers, and decimal metadata can be surprising or unsupported.
  • Run native service and failure tests in your own development environment.

Implementation & operation cost: Additional state and tests must be justified by the demonstrated failure; this lesson is not a requirement for every Go program.

01

The contract and the failure

Token amounts frequently exceed the exact integer range of float64, even when the human-readable value looks small. Converting through a float before math/big permanently loses information. Parse text under an explicit decimal-scale policy, reject unsupported precision and preserve the integer base-unit value. Token metadata is input to validate, not an instruction to allocate unbounded memory.

02

Work through the example

Obtain and verify the correct token contract, network and decimal metadata, then define supported scales and input length. Keep amounts in base units for arithmetic and serialize large integers as exact strings where the wire contract needs them. math/big.Int methods mutate the receiver; use Set for independent copies before sharing values between owners.

go
package main

import (
	"fmt"
	"math/big"
	"strings"
)

func units(text string, scale uint8) (*big.Int, error) {
	if scale > 36 || len(text) > 80 {
		return nil, fmt.Errorf("input budget")
	}
	p := strings.Split(text, ".")
	if len(p) > 2 || p[0] == "" {
		return nil, fmt.Errorf("decimal required")
	}
	frac := ""
	if len(p) == 2 {
		frac = p[1]
		if frac == "" {
			return nil, fmt.Errorf("empty fraction")
		}
	}
	if len(frac) > int(scale) {
		return nil, fmt.Errorf("precision exceeds scale")
	}
	digits := p[0] + frac
	for _, c := range digits {
		if c < '0' || c > '9' {
			return nil, fmt.Errorf("digits only")
		}
	}
	value, ok := new(big.Int).SetString(digits+strings.Repeat("0", int(scale)-len(frac)), 10)
	if !ok || value.BitLen() > 256 {
		return nil, fmt.Errorf("amount out of range")
	}
	return value, nil
}
func main() { n, err := units("1.000000000000000001", 18); fmt.Println(n, err) }
Read the code

The sample produces 1000000000000000001 exactly, without a floating-point intermediate. It rejects signs, exponents, excess fractional precision and values outside its stated 256-bit policy. The 36-decimal cap is this example’s application budget, not a claim that every token uses at most that scale.

03

Production decisions

Symbols are not unique token identifiers, and decimal metadata can be surprising or unsupported. Do not round away an extra fractional digit silently. Test zero, leading zeros, maximum value, too many digits, unsupported scale and the same symbol on different networks. Fees, token transfers and fiat conversions are different quantities with separately defined units and rounding policies.

04

Explain it in an interview

An amount has one more fractional digit than the supported token scale. What should this parser do? Reject it under the explicit precision policy instead of silently rounding or converting through float64; network, token and units must be verified. Describe the state before the operation, its owner, the failure between steps and the evidence you would test. Distinguish what this example demonstrates from the infrastructure a production service still needs.

TAKE THIS WITH YOU

Convert validated decimal text to bounded integer base units with an explicit scale. Reject it under the explicit precision policy instead of silently rounding or converting through float64; network, token and units must be verified.

Check your understanding

An amount has one more fractional digit than the supported token scale. What should this parser do?

Go to the source

Progress stays in this browser. No account needed.