jsnum

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jsnum reads and writes numbers the way JavaScript does.

The writing half covers Number#toString (Format), String(x) (String) and Math.round (Round); the reading half covers parseInt (ParseInt, ParseHex) and parseFloat (ParseFloat). Operations need these to report the same figures CyberChef does, down to where the notation switches to exponential and which way a negative half rounds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(f float64) string

Format writes a float the way JavaScript's Number#toString does. Go's %g agrees on the shortest round-trip digits but switches to exponential notation at different thresholds and spells the exponent differently, so the digits are taken from Go and the presentation is rebuilt here.

func IsHexDigit

func IsHexDigit(c byte) bool

IsHexDigit reports whether c is an ASCII hexadecimal digit.

func IsSpace

func IsSpace(c rune) bool

IsSpace reports whether c is matched by JavaScript's \s class.

func ParseFloat

func ParseFloat(s string) float64

ParseFloat mirrors JavaScript's parseFloat: skip leading whitespace, then parse the longest valid numeric prefix, yielding NaN when none is present.

func ParseHex

func ParseHex(s string) float64

ParseHex is parseInt(s, 16): it reads the leading hex digits and yields NaN when there are none. A 0x prefix is allowed and skipped, as it is for that radix.

func ParseInt

func ParseInt(s string, base int) (int, bool)

ParseInt mimics JS parseInt(s, base) for base 10/16: skips leading whitespace and an optional sign, then consumes valid leading digits. ok is false when no digits are consumed (JS returns NaN).

func Round

func Round(f float64) int

Round rounds half towards positive infinity, as JavaScript's Math.round does. Go's math.Round rounds halves away from zero, which differs for negatives such as -0.5.

func RoundFloat

func RoundFloat(f float64) float64

RoundFloat is Round without the narrowing to int.

func String

func String(f float64) string

String formats a float the way JavaScript's String(x) does: the non-finite values are spelled out, and finite ones use the JSON digits.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL