Menu

No floats near money: how our free estimate template matches the app to the cent
📰

No floats near money: how our free estimate template matches the app to the cent

DEV Community: nextjs·Ridh Grg·2 days ago
#JN1VITdV
#devtonextjs#devto#dev#number#cents
Reading 0:00
15s threshold

CloseQuotely has a free estimate template: a page where a contractor types lines, quantities and prices, sees a total and prints it. No account, nothing saved on our side. It's a marketing page, and it would be easy to write its math in ten minutes with parseFloat .

We didn't, because the app's real quotes use the same lines, and a free page that's a cent off the product is a bad first impression. So the page imports the product's own quote math. Here's what that math does differently.

Floats get money wrong in ordinary cases

parseFloat ( "1.15" ) * 100 // 114.99999999999999 Math . round ( 1.005 * 100 ) / 100 // 1, not 1.01 0.1 + 0.2 // 0.30000000000000004

Enter fullscreen mode

Exit fullscreen mode

The first one is the dangerous one: floor it to get cents and $1.15 becomes $1.14.

Parse text straight into integers

Money is integer cents, and it never passes through a float.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.