Click here to Skip to main content
15,887,135 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
#realJSOP24-Mar-24 11:54
mve#realJSOP24-Mar-24 11:54 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon26-Mar-24 10:42
professionalJeremy Falcon26-Mar-24 10:42 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
rjmoses23-Mar-24 0:00
professionalrjmoses23-Mar-24 0:00 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon23-Mar-24 5:15
professionalJeremy Falcon23-Mar-24 5:15 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
charlieg31-Mar-24 4:31
charlieg31-Mar-24 4:31 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
Stacy Dudovitz23-Mar-24 13:41
professionalStacy Dudovitz23-Mar-24 13:41 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon24-Mar-24 4:47
professionalJeremy Falcon24-Mar-24 4:47 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Stacy Dudovitz24-Mar-24 15:59
professionalStacy Dudovitz24-Mar-24 15:59 
I was a bit alarmed OMG | :OMG: by your reply and solution below:
For this project, I'm in JavaScript/TypeScript and dealing with money. So there is no decimal type. But, after this chat I decided to just add two extra decimal places of resolution. So, I'll store a currency amount as 1.1234 and only round it off to 2 during reporting.

There are two possible solutions:

1) If you are always/only going to traffic in money, a more robust solution would be to use integer math and display formatting.

As an example, the value '$1.23" would be stored in an integer of sufficient size to house the min/max dollar value you wish to traffic in. Using RegEx, it would be trivial to strip off the '$' and '.', yielding a value of the price offset by a factor of 100. To display, you could use simple formatting. You can store the values as-is to a data store, or, if you require marshaling of values, divide the value by 100 by first casting the value to float and dividing by 100f.

In this case, I would use Number or BigInt. A quick search on the largest integer type gives the following results:
The biggest integer type in JavaScript is BigInt. It was introduced in ECMAScript 2020. BigInts can store integers of any size, while the Number type can only store integers between -(2^53 - 1) and 2^53 - 1.

2) You could incorporate decimal.js into your project, which will provide you with the decimal type you seek.

You can find that here:
https://mikemcl.github.io/decimal.js/#Unsure | :~ :text=decimal.-,js,available%20in%20the%20console%20now.

Whichever way you choose, I would implore you NOT to add arbitrary/additional numbers to the right of the decimal place. It will come back to bite you! Cry | :((
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon26-Mar-24 10:41
professionalJeremy Falcon26-Mar-24 10:41 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
mdblack9825-Mar-24 3:13
mdblack9825-Mar-24 3:13 
GeneralWordle 1,006 Pin
StarNamer@work20-Mar-24 14:04
professionalStarNamer@work20-Mar-24 14:04 
GeneralRe: Wordle 1,006 Pin
Amarnath S20-Mar-24 14:08
professionalAmarnath S20-Mar-24 14:08 
GeneralRe: Wordle 1,006 Pin
Sandeep Mewara20-Mar-24 15:42
mveSandeep Mewara20-Mar-24 15:42 
GeneralRe: Wordle 1,006 Pin
Shane010320-Mar-24 17:51
Shane010320-Mar-24 17:51 
GeneralRe: Wordle 1,006 (3/6) Pin
Jeremy Falcon20-Mar-24 19:19
professionalJeremy Falcon20-Mar-24 19:19 
GeneralRe: Wordle 1,006 Pin
GKP199220-Mar-24 19:22
professionalGKP199220-Mar-24 19:22 
GeneralRe: Wordle 1,006 Pin
OriginalGriff20-Mar-24 20:06
mveOriginalGriff20-Mar-24 20:06 
GeneralRe: Wordle 1,006 - 4 4 me Pin
pkfox20-Mar-24 22:15
professionalpkfox20-Mar-24 22:15 
GeneralRe: Wordle 1,006 Pin
Sander Rossel20-Mar-24 22:25
professionalSander Rossel20-Mar-24 22:25 
GeneralRe: Wordle 1,006 Pin
ChandraRam20-Mar-24 23:23
ChandraRam20-Mar-24 23:23 
GeneralRe: Wordle 1,006 Pin
Cp-Coder21-Mar-24 0:02
Cp-Coder21-Mar-24 0:02 
GeneralWhat did one cannibal say to his buddy after eating a clown? Pin
jmaida20-Mar-24 11:37
jmaida20-Mar-24 11:37 
GeneralIf you ever want to see pure, unfiltered joy Pin
honey the codewitch20-Mar-24 8:18
mvahoney the codewitch20-Mar-24 8:18 
GeneralRe: If you ever want to see pure, unfiltered joy Pin
Mike Hankey20-Mar-24 8:31
mveMike Hankey20-Mar-24 8:31 
GeneralRe: If you ever want to see pure, unfiltered joy Pin
jeron120-Mar-24 8:36
jeron120-Mar-24 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.