Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rof

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rof

Determine the most reasonable display format for a given numeric value.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
2
Weekly downloads
 
Created
Source

Rule of Four

Determine the most reasonable display format for a given numeric value or set of numeric values. Using a modified version of the rule of four and other methods.

Introduction

Ideally, when working numeric data, one should know the precision of the underlying data and display those values appropriately. However, when developing software it is not always possible to pre-determine the precision. In addition, when using JavaScript, numbers are not often displayed as one would expect. Take for example the following values and three common methods for generating a display string in JavaScript.

Input ValuetoLocaleStringtoPrecision (N=3)toExponential (N=2)
0.040.040.04004.00e-2
0.20.20.2002.00e-1
20.00012020.02.00e+1
222.002.00e+0
202020.02.00e+1
1.2e+211,200,000,000,000,000,000,0001.20e+211.20e+21
12345678912341,234,567,891,2341.23e+121.23e+12
0.0025550.0030.002552.55e-3
0.0000677700.00006786.78e-5

Some issues we notices are:

  • toLocaleString has inconsistent precision. In many cases (for example 0.00255 and 2.00001) toLocaleString provides too low precision, in others (for example 1234567891234) the precision is unnecessarily high.
  • All three methods make no distinction between integers, decimal, and floating point values.
  • toLocaleString highly has inconsistent string size. For example, displaying 1.2e+21 as a decimal results in an unnecessarily long string.

Additionally, given a set of values, the values should be formatted consistently for comparison to each other.

Therefore, the goals of this project are:

  • Determine the display precision in a predictable manner.
  • Preserve (as much as possible) display for integers, decimals, and floats.
  • Display the smallest string necessary for the value.
  • Given a set of values, determine the most consistent display format across the set.

To this end, this library makes use or the "rule of four" described in Setting number of decimal places for reporting risk ratios: rule of four. The rule of four is a simple method for determining appropriate number of decimal places to use when reporting risk ratios. The rule states “Round the risk ratio to two significant digits if the leading non-zero digit is four or more, otherwise round to three;”. We extend the rule here to other values.

Getting Started

Installing

npm install rof

The core formatting methods (with default options) can be imported directly:

import { ruleOfFour, format, formatInteger, formatDecimal, formatFloat, pickFormat } from '.';

Summary of Methods

ruleOfFour: Formats a value as a decimal using the strict rule of four to determine the precision.

formatFloat: Formats a value as a float (exponential) using the rule of four to determine the precision.

formatInteger: Formats a value as an integer or float (using formatFloat) for large values.

formatDecimal: Formats a value as a decimal or float (using formatFloat) for large or small values.

format: Formats a value as an integer (using formatInteger) or a decimal (using formatDecimal) if the value is not an integer.

pickFormat: Given an array of values, returns the best formatter method, from among the following three methods: formatFloat, formatInteger, and formatDecimal.

API Reference

ruleOfFour(x: number): string

Formats a value as a decimal using the strict rule of four to determine the precision.

Input ValuetoLocaleStringtoPrecision (N=3)Rule of Four
0.040.040.04000.040
0.20.20.2000.200
0.40.40.4000.40
2.0000122.002.00
4.0000144.004.0
20.00012020.020.0
40.00014040.040
222.002.00
444.004.0
202020.020.0
404040.040
000.000.00
-000.000.00
InfinityInfinityInfinity
-Infinity-∞-Infinity-Infinity

formatFloat(x: number): string

Returns the value formatted as a floating point value using the following rules:

  • If x === -0 returns -0.00e+0
  • otherwise returns x.toExponential(N) (where N is the precision following the rule of four)
Input ValuetoLocaleStringtoPrecision (N=3)Rule of FourformatFloat
0.040.040.04000.0404.0e-2
0.20.20.2000.2002.00e-1
0.40.40.4000.404.0e-1
2.0000122.002.002.00e+0
4.0000144.004.04.0e+0
20.00012020.020.02.00e+1
40.00014040.0404.0e+1
222.002.002.00e+0
444.004.04.0e+0
202020.020.02.00e+1
404040.0404.0e+1
000.000.000.00e+0
-000.000.000.00e+0
InfinityInfinityInfinityInfinity
-Infinity-∞-Infinity-Infinity-Infinity

formatInteger(x: number): string

Returns the value formatted as an integer using with the following rules:

  • If the Math.round(x) === -0 returns -0
  • if the resulting character length is <= 9 returns Math.round(x).toLocaleString()
  • otherwise, formatFloat(Math.round(x))
Input ValuetoLocaleStringtoPrecision (N=3)Rule of FourformatInteger
0.040.040.04000.0400
0.20.20.2000.2000
0.40.40.4000.400
2.0000122.002.002
4.0000144.004.04
20.00012020.020.020
40.00014040.04040
222.002.002
444.004.04
202020.020.020
404040.04040
000.000.000
-000.000.00-0
InfinityInfinityInfinity
-Infinity-∞-Infinity-Infinity-∞

formatDecimal(x: number): string

Returns the value formatted as a decimal using with the following rules:

  • If the value is -0, returns -0.00
  • if Math.abs(x) < 0.4 and the resulting character length is <= 9, returns x.toPrecision(N) (where N is the precision following the rule of four)
  • if Math.abs(x) >= 0.4 and the resulting character length is <= 9, returns x.toLocaleString(locales, {minimumFractionDigits: 2, maximumFractionDigits: 2})
  • otherwise, returns formatFloat(x).
Input ValuetoLocaleStringtoPrecision (N=3)Rule of FourformatDecimal
0.040.040.04000.0400.040
0.20.20.2000.2000.200
0.40.40.4000.400.40
2.0000122.002.002.00
4.0000144.004.04.00
20.00012020.020.020.00
40.00014040.04040.00
222.002.002.00
444.004.04.00
202020.020.020.00
404040.04040.00
000.000.000.00
-000.000.00-0.00
InfinityInfinityInfinity
-Infinity-∞-Infinity-Infinity-∞

format(x: number): string

Returns the value formatted with the following rules:

  • If x is an integer (within a given threshold) returns formatInteger(x) .
  • otherwise, returns formatDecimal(x).
Input ValuetoLocaleStringtoPrecision (N=3)Rule of Fourformat
0.040.040.04000.0400.040
0.20.20.2000.2000.200
0.40.40.4000.400.40
2.0000122.002.002.00
4.0000144.004.04.00
20.00012020.020.020.00
40.00014040.04040.00
222.002.002
444.004.04
202020.020.020
404040.04040
000.000.000
-000.000.00-0
InfinityInfinityInfinity
-Infinity-∞-Infinity-Infinity-∞

Other sample outputs below:

Integers
Input ValuetoLocaleStringtoPrecision (N=3)Rule of Fourformat
111.001.001
000.000.000
444.004.04
234234234234234
396396396396396
37283,7283.73e+33.73e+33,728
5115115115.1e+2511
21562,1562.16e+32.16e+32,156
8523085,2308.52e+48.5e+485,230
428950428,9504.29e+54.3e+5428,950
138612138,6121.39e+51.39e+5138,612
28260582,826,0582.83e+62.83e+62,826,058
79840857,984,0857.98e+68.0e+67,984,085
3954262439,542,6243.95e+73.95e+73.95e+7
1944859419,448,5941.94e+71.94e+71.94e+7
498762759498,762,7594.99e+85.0e+85.0e+8
-5-5-5.00-5.0-5
-4-4-4.00-4.0-4
-18-18-18.0-18.0-18
-467-467-467-4.7e+2-467
-360-360-360-360-360
-1997-1,997-2.00e+3-2.00e+3-1,997
-5302-5,302-5.30e+3-5.3e+3-5,302
-33667-33,667-3.37e+4-3.37e+4-33,667
-86551-86,551-8.66e+4-8.7e+4-86,551
-412564-412,564-4.13e+5-4.1e+5-412,564
-499362-499,362-4.99e+5-5.0e+5-499,362
-1497968-1,497,968-1.50e+6-1.50e+6-1.50e+6
-2995607-2,995,607-3.00e+6-3.00e+6-3.00e+6
-30457267-30,457,267-3.05e+7-3.05e+7-3.05e+7
-63377713-63,377,713-6.34e+7-6.3e+7-6.3e+7
-314199248-314,199,248-3.14e+8-3.14e+8-3.14e+8
Decimals and Floats
Input ValuetoLocaleStringtoPrecision (N=3)Rule of Fourformat
0.7674117391822990.7670.7670.770.77
0.165089136247652530.1650.1650.1650.165
9.5695964294958789.579.579.69.57
44.63366844950202544.63444.64544.63
66.3605899319378166.36166.46666.36
236.50398366071715236.504237237236.50
385.04081126722724385.041385385385.04
2012.68584969208312,012.6862.01e+32.01e+32,012.69
2355.32533596210622,355.3252.36e+32.36e+32,355.33
3883.81322561633943,883.8133.88e+33.88e+33,883.81
29654.12967505167829,654.132.97e+42.97e+429,654.13
423219.76370784326423,219.7644.23e+54.2e+54.2e+5
682694.689384022682,694.6896.83e+56.8e+56.8e+5
4936032.2320836354,936,032.2324.94e+64.9e+64.9e+6
3924507.92875289173,924,507.9293.92e+63.92e+63.92e+6
1121775.09084551151,121,775.0911.12e+61.12e+61.12e+6
10578661.86702727310,578,661.8671.06e+71.06e+71.06e+7
389792236.9268605389,792,236.9273.90e+83.90e+83.90e+8
0.0303083510456531040.030.03030.03030.0303
0.379892481986381150.380.3800.3800.380
0.0091266324160222970.0090.009130.00910.0091
0.0209240214526344660.0210.02090.02090.0209
0.00004997426833902496400.00005000.0000500.000050
0.0024454540924932520.0020.002450.002450.00245
0.0000667007898038703800.00006670.0000670.000067
0.0002326315234746369200.0002330.0002330.000233
0.00000822885741466317800.000008230.00000820.0000082
0.00002861405194473154700.00002860.00002860.0000286
6.667119786706243e-706.67e-76.7e-76.7e-7
5.8605715503222685e-805.86e-85.9e-85.9e-8
1.8237805354572288e-801.82e-81.82e-81.82e-8
7.49612642450942e-807.50e-87.5e-87.5e-8
9.327911102521533e-909.33e-99.3e-99.3e-9
1.181162832355276e-901.18e-91.18e-91.18e-9
-0.7600695370948154-0.76-0.760-0.76-0.76
-1.781100859767616-1.781-1.78-1.78-1.78
-8.754569639736625-8.755-8.75-8.8-8.75
-3.2390801713915063-3.239-3.24-3.24-3.24
-17.8859938122373-17.886-17.9-17.9-17.89
-130.5215974441648-130.522-131-131-130.52
-324.5343291038001-324.534-325-325-324.53
-3554.46555809074-3,554.466-3.55e+3-3.55e+3-3,554.47
-3411.666120331296-3,411.666-3.41e+3-3.41e+3-3,411.67
-43599.62195662187-43,599.622-4.36e+4-4.4e+4-4.4e+4
-98189.53648882844-98,189.536-9.82e+4-9.8e+4-9.8e+4
-357442.9233516688-357,442.923-3.57e+5-3.57e+5-3.57e+5
-464747.87306420476-464,747.873-4.65e+5-4.6e+5-4.6e+5
-131706.4677680402-131,706.468-1.32e+5-1.32e+5-1.32e+5
-599752.080929028-599,752.081-6.00e+5-6.0e+5-6.0e+5
-10356429.76627571-10,356,429.766-1.04e+7-1.04e+7-1.04e+7
-67522686.27283327-67,522,686.273-6.75e+7-6.8e+7-6.8e+7
-446240278.38519365-446,240,278.385-4.46e+8-4.5e+8-4.5e+8
-0.047570797733379494-0.048-0.0476-0.048-0.048
-0.40606861218027934-0.406-0.406-0.41-0.41
-0.0037719778035773446-0.004-0.00377-0.00377-0.00377
-0.0005854655216267713-0.001-0.000585-0.00059-0.00059
-0.0009629408942679378-0.001-0.000963-0.00096-0.00096
-0.002278157157351423-0.002-0.00228-0.00228-0.00228
-0.000055188346473247886-0-0.0000552-0.000055-0.000055
-0.00046808214910745374-0-0.000468-0.00047-0.00047
-0.00000763481366128597-0-0.00000763-0.0000076-7.6e-6
-0.000024127446661277244-0-0.0000241-0.0000241-2.41e-5
-2.2228413638871935e-7-0-2.22e-7-2.22e-7-2.22e-7
-2.652616482513714e-7-0-2.65e-7-2.65e-7-2.65e-7
-7.727311790760662e-8-0-7.73e-8-7.7e-8-7.7e-8
-3.6526232386466837e-7-0-3.65e-7-3.65e-7-3.65e-7
-9.78731507500792e-9-0-9.79e-9-9.8e-9-9.8e-9
-2.195914860919613e-8-0-2.20e-8-2.20e-8-2.20e-8
Special
Input ValuetoLocaleStringtoPrecision (N=3)Rule of Fourformat
NaNNaNNaNNaNNaN
1 + 233.003.003
(0.1 + 0.2)*1033.003.003.00
PI3.1423.143.143.14
E2.7182.722.722.72
SQRT21.4141.411.411.41
EPSILON02.22e-162.22e-162.22e-16

pickFormat(arr: number[]): function

Given an array of values, returns the best formatting method using the following rules:

  • If all values are integers:
    • and the maximum Log10 of the values is <= 6, returns formatInteger.
    • otherwise, returns formatFloat.
  • If not all values are integers:
    • and the maximum Log10 of the values is >= 6 or minimum Log10 of the values <= -6, returns formatFloat
    • otherwise, returns formatDecimal

RofFormat class

The RofFormat object is a constructor encapsulating the methods above and allowing customization.

new RofFormat(locales?: string | string[], options?: RofFormatOptions)

locales parameter is a string or array passed to internal usage of NumberFormat.

options operator is an object with some or all of the following properties:

minimumSignificantDigits The minimum number of integer digits to use. The default is 2.

maximumSignificantDigits The maximum number of significant digits to use. The default is minimumSignificantDigits + 1

integerThreshold The threshold used to determine if a value is an integer. The default is Number.EPSILON.

Other options values are passed to internal usage of NumberFormat.

License

This project is licensed under the MIT License - see the LICENSE file for details

FAQs

Package last updated on 15 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc