5 ms·
Show HN: dnum − A small TypeScript library to handle decimal numbers
- meagher 3y agobig fan of this library!
- bpierre 3y ago<3
- guideamigo_com 3y agoAnother day. Another Typescript library that does what languages like Java, Python, and Go have for decades!
- revskill 3y agoWait, you can use Java, Python, and Go in browser and the edge ?
- DarkNova6 3y agoOne can only hope for webassembly...
- JodieBenitez 3y agoDon't know about Java or Go, but Python... yes. https://codepen.io/johndodo/pen/ExGyWbx https://codepen.io/johndodo/pen/ExGyWbx
- AA-BA-94-2A-56 3y agoYou can compile Java and Go to web assembly and use those wherever you would have used JavaScript.
- cbm-vic-20 3y agoWe've had Java in the browser since the '90s.
- quickthrower2 3y agoUntil 2015
- deleted 3y ago[deleted]
- williamdclt 3y agoI’m not sure what’s your point? That JS shouldn’t have this library because other languages did it first a while ago? Should concepts only be implemented in one language because after that it’s rediscovering knowledge rather than being inventive? I also really dislike the undertone of « other languages are better », which is naive at best.
- AA-BA-94-2A-56 3y agoThis. Fixed-point decimal isn't even implemented in standard Rust– which I would deem a very mature language, because they want to keep the standard library quite small. If you want to use fixed-point decimal, there's a crate for that; https://crates.io/crates/rust_decimal https://crates.io/crates/rust_decimal
- mijamo 3y agoRust standard library is very limited and IMO a huge downside of the language, especially as the third party libraries to replace it are not up to par and also often the community has not agreed on one single standard so writing higher level libraries forces you to pick one side of the ecosystem
- AA-BA-94-2A-56 3y agoI'm curious where you got this information from? How do you know the third-party libraries are not up to par? Is this personal or second-hand experience? Is it an assumption?
- deleted 3y ago[deleted]
- meepmorp 3y agoWe can have nice things in multiple languages.
- iudqnolq 3y agoI recently learned a very cute technique for storing arbitrary precision decimals between 0 and 1 as strings. This is useful for representing the index of items in a reorderable list because you can always make a new index in between two existing indexes (so long as you ensure each index is unique). Creating a new index in between two indices and comparing two indices are both O(N), but the cute part is that the standard string comparison in JavaScript just works. You just encode the digits after the decimal point. As in for "0.15" you encode "15". Except you do it in a big base, and to support concurrent edits you add some randomness to the midpoint operation (because this falls apart if two items have identical indexes) I learned it from Evan Wallace's Blog (Figma, Esbuild). https://madebyevan.com/algos/crdt-fractional-indexing/ https://madebyevan.com/algos/crdt-fractional-indexing/
- joshxyz 3y agoreminds me of big.js and decimal.js, working with decimals is really fun apecially on apps involving money. no room for errors!
- sakesun 3y agoI want operator overloading in JS or perhaps TS. :(
- zaphirplane 3y agothrow new Error("dnum: decimals cannot be negative"); Does this mean what is says ? The library supports positive decimals only ?
- bpierre 3y agoI should probably word this better. Numbers are represented in dnum using a pair of integer + precision decimals. For example, this is the number 1.0 with a precision of 18 decimals: [1000000000000000000n, 18] A number cannot have less decimals than none, so this is why this error exists.
- quickthrower2 3y agoThat is a first step towards an open source version of https://news.ycombinator.com/item?id=37328669 https://news.ycombinator.com/item?id=37328669