Converting decimal text to a binary floating-point number and multiplying afterward can lose the exact representation the user supplied. If a field promises a fixed scale, parse the text into integer units directly. Define excess precision as an explicit validation outcome rather than letting a conversion choose an undocumented rounding rule.
The example uses a synthetic nonnegative quantity with two decimal places and a twelve-digit maximum whole part. It runs in Node.js 24. It does not define real currency metadata, exchange conversion, taxation, or rounding policy; those require separate domain contracts.
Choose the scale and grammar first
For this field, 12, 12.3, and 12.30 represent 1200, 1230, and 1230 integer units. The grammar permits a dot as the decimal separator and one or two fractional digits when the dot is present. It rejects signs, exponent notation, grouping separators, surrounding whitespace, and leading zeros other than a single zero.
These are interface choices. A localized input widget may accept a different display format, but it must translate through a documented boundary before sending the canonical representation. Do not strip all punctuation and hope that the remaining digits preserve the user's intent. The same visible separator can have different meanings in different locales.
The ECMAScript BigInt specification supplies an integer representation for the resulting units. It does not assign a decimal scale or a unit of measurement. Keep the unit and scale in the schema so that another field's integer value cannot be mistaken for the same quantity merely because both use BigInt.
Parse the decimal characters without a Number intermediate
Place this original parser in a Node.js module. It returns null for an invalid input and an integer for an accepted one:
function parseHundredths(text) {
if (typeof text !== "string" || text.length === 0 || text.length > 15) return null;
const match = /^(0|[1-9][0-9]{0,11})(?:\.([0-9]{1,2}))?$/.exec(text);
if (match === null || match[0] !== text) return null;
const fraction = (match[2] ?? "").padEnd(2, "0");
return BigInt(match[1]) * 100n + BigInt(fraction);
}
The maximum accepted text has twelve whole digits, a dot, and two fractional digits. The full-match comparison also rejects a trailing line terminator that an end anchor can otherwise leave outside its match. The bounded grammar prevents an arbitrarily large integer string from reaching the conversion.
Padding operates only on the accepted fractional portion: one digit becomes tens of hundredths, and an absent fraction becomes zero. The implementation does not first approximate the whole input as a Number. It constructs the result from digit strings and integer arithmetic.
This parser deliberately rejects 1.234. Changing that outcome to 123 units would introduce a rounding or truncation operation. If the product requires that behavior, expose it as another named conversion with its own rule and test cases rather than silently expanding the parser's responsibility.
Check precision and refusal independently
Append these assertions and run the module with Node.js. The 1.15 case also demonstrates the tempting Number-first truncation failure: its binary approximation multiplied by 100 falls below 115, so truncation produces 114. Direct decimal parsing preserves the intended 115 units without repairing an earlier approximation.
import assert from "node:assert/strict";
assert.equal(parseHundredths("0"), 0n);
assert.equal(parseHundredths("0.01"), 1n);
assert.equal(parseHundredths("12.3"), 1230n);
assert.equal(parseHundredths("12.30"), 1230n);
assert.equal(parseHundredths("999999999999.99"), 99999999999999n);
assert.equal(Math.trunc(Number("1.15") * 100), 114);
assert.equal(parseHundredths("1.15"), 115n);
for (const value of ["", "01.00", "1.234", "-1", "+1", "1e2", "1,00", "1.", " 1", "1.00\n", "1000000000000"]) {
assert.equal(parseHundredths(value), null);
}
assert.equal(parseHundredths(12.3), null);
The numeric-input rejection preserves the interface boundary. By the time a caller provides a floating-point value, the original decimal text may no longer be available. Accepting both representations in one helper makes it harder to identify which path supplied an exact value and which path already performed a conversion.
Test the maximum independently from ordinary values. A database column or downstream protocol can have a smaller range than the parser's integer type. Add that domain range check before storage or side effects. A type capable of representing the value does not establish that the business operation permits it.
For arithmetic that produces fractions, such as allocating a quantity among three recipients, define how remainders are distributed. Parsing exact input cannot decide that later operation. Verify conservation of total units and the tie-breaking rule rather than asserting only that each result looks plausible in a formatted display.
Preserve the contract across serialization and storage
Use a documented wire representation such as an integer-unit decimal string plus an explicit unit, or canonical fixed-scale decimal text. Ordinary JSON serialization does not directly support a bare BigInt; the JSON serialization algorithm specifies an error for that primitive without an appropriate conversion path. Choose the representation deliberately instead of adding a global serialization patch.
Avoid converting the integer units back to Number merely to pass through another layer. Check that layer's exact-integer limits and accepted format. If it accepts a decimal string, send the declared scale and test the round trip through the actual adapter. A local parser test does not establish database-driver behavior.
At the server boundary, validate again before committing changes. A browser widget's friendly formatting cannot authorize a negative value, a different unit, or an over-range quantity. For denied input, preserve stored state and return a bounded field error that lets the user correct the value.
When changing the scale later, version the contract or migrate records with an explicit interpretation. Multiplying old units by a new factor without knowing their original scale can corrupt meaning while preserving syntactically valid integers. Keep one accepted and one rejected example in the API documentation so every producer can verify the same representation before its values acquire persistent effects.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit