1.0.0 • Published 4 years ago
precise-float v1.0.0
precise-float
precise-float
's primary use is seeing the full decimal expansion of arbitrary JavaScript numbers.
const preciseFloat = require('precise-float')
String(0.1)
// '0.1'
preciseFloat.stringify(0.1)
// '0.1000000000000000055511151231257827021181583404541015625'
String(90071992547409904)
// '90071992547409900'
preciseFloat.stringify(90071992547409904)
// '90071992547409904'
String(Number.MAX_VALUE)
// '1.7976931348623157e+308'
preciseFloat.stringify(Number.MAX_VALUE)
// '179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368'
String(Number.MIN_VALUE)
// '5e-324'
preciseFloat.stringify(Number.MIN_VALUE)
// '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625'
You can also use precise-float
to parse string representations of JavaScript numbers, throwing an exception if that string isn't precisely representable as such.
Number.parseFloat('1e23')
// 99999999999999991611392
preciseFloat.parse('1e23')
// throws an exception
preciseFloat.parse('99999999999999991611392')
// 99999999999999991611392
Installation
npm install precise-float