TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: dnum − A small TypeScript library to handle decimal numbers

42 pointsby bpierreover 1 year ago

7 comments

iudqnolqover 1 year ago
I 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).<p>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.<p>You just encode the digits after the decimal point. As in for &quot;0.15&quot; you encode &quot;15&quot;. 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)<p>I learned it from Evan Wallace&#x27;s Blog (Figma, Esbuild). <a href="https:&#x2F;&#x2F;madebyevan.com&#x2F;algos&#x2F;crdt-fractional-indexing&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;madebyevan.com&#x2F;algos&#x2F;crdt-fractional-indexing&#x2F;</a>
quickthrower2over 1 year ago
That is a first step towards an open source version of <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37328669">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37328669</a>
joshxyzover 1 year ago
reminds me of big.js and decimal.js, working with decimals is really fun apecially on apps involving money. no room for errors!
sakesunover 1 year ago
I want operator overloading in JS or perhaps TS. :(
zaphirplaneover 1 year ago
throw new Error(&quot;dnum: decimals cannot be negative&quot;);<p>Does this mean what is says ? The library supports positive decimals only ?
评论 #37348643 未加载
meagherover 1 year ago
big fan of this library!
评论 #37344227 未加载
guideamigo_comover 1 year ago
Another day.<p>Another Typescript library that does what languages like Java, Python, and Go have for decades!
评论 #37344038 未加载
评论 #37345997 未加载
评论 #37343829 未加载
评论 #37344020 未加载
评论 #37344171 未加载