0

I have a piece of code that is pulling data from a SharePoint list from I'm using SPServices and this code then pulls from the Spent column and adds up all the numbers and then puts it in an array however I'm getting some weird results.

none of the numbers it pulls through have more than 2 decimal places(it's a finance column) however I'm getting totals like 57062.229999999996 and 151704.58000000002

I've tried using .toFixed(2) on to mynumber and parseFloat(SpentFix) and then converting back to a number and I get the same results.

Also the reason why I have a .substring(7) is because the spend column is a computed field so when pulling data float,# is a the start of each entry for some reason

if anyone can help that would be great.

var SpentFix = $(this).attr("ows_Spent").substring(7);
if (SpentFix != undefined) {
    var mynumber = parseFloat(SpentFix)
    SLarray[counter][1] += mynumber;   
}                                                       
adam Wadsworth
  • 774
  • 1
  • 8
  • 26
  • 2
    Work with INTEGERS if you work with money - 100 cents instead of 1 dollar (do everything in cents). Add a decimal point only when you output something to users. Read up on numbers in [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) format (Javascript is not the only language using it)! If you don't have any fractions integers up to [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) are perfectly safe in Javascript. As soon as you add a fractional part things become too unreliable for financial computations. – Mörre Oct 20 '17 at 10:11
  • I don't really understand what you are saying (sorry I'm still new at all this) This is outputting to users the SLarray[counter][1] is then written to a web page it's meant to show the spend of a project/department etc. but 57062.22999999996 is not really useful and 57062 is not accurate enough. – adam Wadsworth Oct 20 '17 at 10:15
  • 1
    Also check out https://stackoverflow.com/questions/2876536/precise-financial-calculation-in-javascript-what-are-the-gotchas for a lengthier explanation. – Mörre Oct 20 '17 at 10:16
  • 3
    _"I don't really understand what you are saying"_ -- What is there to understand? Use cents, not dollars (same for all other currencies)! Just DON'T USE FRACTIONS! – Mörre Oct 20 '17 at 10:18
  • ah now I understand what you mean and it would be penny not pounds over here :) I could do as you say but it would require formatting the data before inputting it into SharePoint but that could solve my issue. Thanks – adam Wadsworth Oct 20 '17 at 10:21
  • You can use a library for currency calculations: https://github.com/scurker/currency.js/ – Tomalak Oct 20 '17 at 10:22
  • Divide by 100 just before output (not before!), or convert into string (which has to happen implicitly anyway) and insert a '.' (dot) at the -2nd position for output. I would use the suggested library only if you actually need it -- the simple advice you already have is...well, simple, and sufficient if you only work with one currency and don't need the added formatting flexibility (various currency systems) of the library. Keep (library) dependencies small! – Mörre Oct 20 '17 at 10:23

0 Answers0