MCPcopy Create free account
hub / github.com/denoland/std / serializeDecimal

Function serializeDecimal

http/unstable_structured_fields.ts:1283–1313  ·  view source on GitHub ↗
(value: number)

Source from the content-addressed store, hash-verified

1281}
1282
1283function serializeDecimal(value: number): string {
1284 if (!Number.isFinite(value)) {
1285 throw new TypeError("Decimal must be finite");
1286 }
1287
1288 // Round to MAX_DECIMAL_FRACTIONAL_DIGITS decimal places
1289 const scale = 10 ** MAX_DECIMAL_FRACTIONAL_DIGITS;
1290 const rounded = Math.round(value * scale) / scale;
1291
1292 // Check integer part (max MAX_DECIMAL_INTEGER_DIGITS digits)
1293 const intPart = Math.trunc(Math.abs(rounded));
1294 if (intPart > MAX_DECIMAL_INTEGER_PART) {
1295 throw new TypeError("Decimal integer part too large");
1296 }
1297
1298 // Format with MAX_DECIMAL_FRACTIONAL_DIGITS fractional digits
1299 const str = rounded.toFixed(MAX_DECIMAL_FRACTIONAL_DIGITS);
1300
1301 // Remove trailing zeros but keep at least one digit after decimal
1302 let end = str.length;
1303 while (end > 0 && str[end - 1] === "0") {
1304 end--;
1305 }
1306 // Keep at least one digit after decimal point
1307 const dotIndex = str.indexOf(".");
1308 if (end <= dotIndex + 1) {
1309 end = dotIndex + 2;
1310 }
1311
1312 return str.slice(0, end);
1313}
1314
1315function serializeString(value: string): string {
1316 // Validate ASCII printable and check if escaping needed

Callers 1

serializeBareItemFunction · 0.85

Calls 1

sliceMethod · 0.45

Tested by

no test coverage detected