Go's strconv.ParseFloat in TypeScript

✓ Verified: Go 1.23
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { ParseFloat } from 'locutus/golang/strconv/ParseFloat'.

Or with CommonJS: const { ParseFloat } = require('locutus/golang/strconv/ParseFloat')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1ParseFloat('3.14159', 64)[0]3.14159
2ParseFloat('-1.5e2', 64)[0]-150
3ParseFloat('bad', 64)[0]0
4ParseFloat('99999999999999974834176', 64)[0]9.999999999999997e+22

Notes

  • Parses a floating-point number from string input. Returns [value, null] on success, [0, error] on invalid syntax.

  • Includes edge cases adapted from Go’s src/strconv/atof_test.go.

Here's what our current TypeScript equivalent to Go's strconv.ParseFloat looks like.

export function ParseFloat(s: string, bitSize: number): [number, Error | null] {
// discuss at: https://locutus.io/golang/strconv/ParseFloat
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Parses a floating-point number from string input.
// note 1: Returns [value, null] on success, [0, error] on invalid syntax.
// note 2: Includes edge cases adapted from Go's src/strconv/atof_test.go.
// example 1: ParseFloat('3.14159', 64)[0]
// returns 1: 3.14159
// example 2: ParseFloat('-1.5e2', 64)[0]
// returns 2: -150
// example 3: ParseFloat('bad', 64)[0]
// returns 3: 0
// example 4: ParseFloat('99999999999999974834176', 64)[0]
// returns 4: 9.999999999999997e+22

const value = String(s).trim()
const bits = Math.trunc(Number(bitSize))

if (bits !== 32 && bits !== 64) {
return [0, new Error('strconv.ParseFloat: invalid bitSize')]
}

if (/^[+-]?(?:inf(?:inity)?|nan)$/i.test(value)) {
return [Number(value), null]
}

const isValidFloatLiteral = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/
if (!isValidFloatLiteral.test(value)) {
return [0, new Error(`strconv.ParseFloat: parsing "${value}": invalid syntax`)]
}

const parsed = Number.parseFloat(value)
if (Number.isNaN(parsed)) {
return [0, new Error(`strconv.ParseFloat: parsing "${value}": invalid syntax`)]
}

return [bits === 32 ? Math.fround(parsed) : parsed, null]
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


Help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More Go strconv functions


Star