JS calculations (not DFFS)

Forums General discussion JS calculations (not DFFS)

This topic contains 1 reply, has 2 voices, and was last updated by  Alexander Bautz 1 week, 5 days ago.

  • Author
    Posts
  • #25621

    Maciek Grischke
    Participant

    I noticed a strange behaviour when calculating in JS.

    One user submitted a form today with the following values:

    var one = 14.8;
    var two = 7.4;
    var three = 29.6;
    var four = 5;
    var total = one+two+three+four;

    The result is: 56.800000000000004 instead of 56.8.

    I know I can use match.round, but I’d like to understand what is actually happening.
    I saw similar behaviour in Microsoft Flow.

    This seems to be happening when .2 increments are used.

  • #25631

    Alexander Bautz
    Keymaster

    Hi,
    This is how JavaScript handles floating point numbers – you find some more info here, but it’s quite complicated (and it’s way over my head…) – the simple solution is to use .toFixed(2) to truncate the number to two decimals.

    Alexander

You must be logged in to reply to this topic.