-1

I have some data like RM 28.51 (Currency), RM30.28(Currency) in my table and need to use these data to do some calculation

my solution is to parseFloat(28.51).tofixed(2) + parseFloat(30.28).tofixed(2)

but when the moment more and more data come in , sometimes they will some decimal point error shown as result

Example my result at the end will show 3859.39 but the actual is 3859.40

Is there any better solution?

  • 2
    `parseFloat(28.51).tofixed(2) + parseFloat(30.28).tofixed(2)` - this won't add the numbers because `.toFixed()` returns a string. Also why do you call `parseFloat()` with numbers? – Andreas Mar 03 '22 at 08:28
  • The more single rounded numbers you add the bigger the loss of data becomes. If table refers to an actual database, I recommend to do your calculations on it as well. – Lain Mar 03 '22 at 08:34
  • above is some example, sometimes the data is string. so as @Lain mention, I cant use parseFloat.toFixed right – Hong Ernest Mar 03 '22 at 08:35
  • 1
    or should I use Math.round(someNumber * 1e2) / 1e2 instead of var someNumber = 123.456; someNumber = parseFloat(someNumber.toFixed(2)); – Hong Ernest Mar 03 '22 at 08:38
  • One ancient way used to be a multiplication to whole numbers and division back to decimals. Like `(parseFloat('28.51')*1000 + parseFloat('30.28')*1000)/1000`. – Lain Mar 03 '22 at 08:39
  • 1
    You should not use floating-point number if you want to represent decimals: [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – t.niese Mar 03 '22 at 09:18

1 Answers1

1

parseFloat(28.51).toFixed(2) + parseFloat(30.28).toFixed(2) - sometimes they will some decimal point error shown as result

The JavaScript toFixed method takes in a number and outputs a string

When you use + with two numbers, you add both numbers.

When you use + with two strings, you join both strings together.

The example below illustrates the difference.

function addTwoStrings(value1, value2) {
  return parseFloat(value1).toFixed(2) + parseFloat(value2).toFixed(2)
}
function addTwoNumbers(value1, value2) {
  return (parseFloat(value1) + parseFloat(value2)).toFixed(2)
}

console.log(addTwoStrings(28.514, 30.284))
console.log(addTwoNumbers(28.514, 30.284))
Zach Jensz
  • 2,608
  • 3
  • 8
  • 23
  • I think OP's problem is more to do with floating point precision in JavaScript. – phuzi Mar 03 '22 at 08:54
  • @phuzi Oops read *that* wrong – Zach Jensz Mar 03 '22 at 08:56
  • ```const value1 = 28.51 const value2 = '30.28' const value3 = +value1 + +value2 function valueToCurrencyString(value) { return parseFloat(value).toLocaleString('en', { minimumFractionDigits: 2, maximumFractionDigits: 2, style: 'currency', currency: 'MYR' }); } console.log(valueToCurrencyString(value3)); newNumber = parseFloat(value3).toFixed(2); console.log(newNumber); ``` sorry am a newbie, but is that newNumber any dffirence with console.log(valueToCurrencyString(value3)); – Hong Ernest Mar 03 '22 at 08:59
  • Sorry @HongErnest I don't understand that question – Zach Jensz Mar 03 '22 at 09:01
  • ohh sorry, I means If I parseFloat(value3).toFixed(2), what is the difference to console.log(valueToCurrencyString(value3)); – Hong Ernest Mar 03 '22 at 09:04
  • @HongErnest I have massively improved the answer please take another read :) – Zach Jensz Mar 03 '22 at 09:14