npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

json-float

v1.0.0

Published

Represent any floating point value as JSON

Downloads

95

Readme

json-float

Represent any floating point value as JSON.

:white_check_mark: Node 18+
:white_check_mark: ESM or CJS :white_check_mark: TypeScript-Native
:white_check_mark: Comprehensive Documentation
:white_check_mark: 100% Coverage

:heart: :evergreen_tree: Made with love in Portland, Oregon. :evergreen_tree: :heart:

Getting started

npm install json-float

Emitting JSON:

import { replaceFloat, reviveFloat } from "./index.ts";

const o = {
  normalFloat: 1.2,
  specialFloat: Infinity,
  otherValue: {
    key: "string",
  },
};

const s = JSON.stringify(o, replaceFloat, 2);
/*
{
  "normalFloat": 1.2,
  "specialFloat": {
    "type": "ieee754",
    "value": "Infinity"
  },
  "otherValue": {
    "key": "string"
  }
}
*/
const p = JSON.parse(s, reviveFloat); // Returns an identical object

For more fine-grained control, see float2json, json2float, and hasJsonRepresentation below.

Why?

The IEEE 754 floating-point values Infinity, -Infinity, -0, and NaN cannot be represented as a literal JSON numbers. JSON.stringify renders the value -0 as 0 (numerically the same but with a different byte-level representation) and renders the other three as null. json-float converts floating point values to and from a JSON-serializable object representation so all possible values can be stored.

JSON Representation

Example outputs from float2json:

{
  type: 'ieee754',
  value: 'NaN',
}

{
  type: 'ieee754',
  value: 23.56787
}

type

Always the string ieee754.

value

One of the following:

  • Any of the strings Infinity, -Infinity, -0, or NaN: represents the corresponding floating point value
  • A JavaScript number.

API Reference

replaceFloat

Implements the replacer interface of JSON.stringify.

Example:

const o = {
  normalFloat: 1.2,
  specialFloat: Infinity,
  otherValue: {
    key: "string",
  },
};

const s = JSON.stringify(o, replaceFloat, 2);
/*
{
  "normalFloat": 1.2,
  "specialFloat": {
    "type": "ieee754",
    "value": "Infinity"
  },
  "otherValue": {
    "key": "string"
  }
}
*/

reviveFloat

Implements the reviver interface of JSON.parse.

Example:

const s = `
{
  "normalFloat": 1.2,
  "specialFloat": {
    "type": "ieee754",
    "value": "Infinity"
  },
  "otherValue": {
    "key": "string"
  }
}`;
const p = JSON.parse(s, reviveFloat);
/*
{
  normalFloat: 1.2,
  specialFloat: Infinity,
  otherValue: {
    key: 'string',
  },
}
*/

hasJsonRepresentation

Example:

hasJsonRepresentation(1.1); // true
hasJsonRepresentation(NaN); // false

Parameters:

  • n: number.

Returns: true if the number can be represented directly with a JSON number, false otherwise.

float2json

Wraps a number in a JSON object.

Example:

float2json(1.1); // { type: 'ieee754', value: 1.1 }
float2json(NaN); // { type: 'ieee754', value: NaN }

Parameters:

  • n: number.

Returns: JSON float object (see JSON Representation). The number is wrapped even if it could be rendered directly as a JSON number.

json2float

Unwraps a JSON float object to a number.

Example:

json2float({ type: "ieee754", value: 1.1 }); // 1.1
json2float({ type: "ieee754", value: NaN }); // NaN

Parameters:

License

MIT. See LICENSE.txt.

Contributing

See CONTRIBUTING.