2

I have a code in JavaScript

shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber

returns number as 6655.866558 so I cover it in parentheses like:

(shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber)

now it returns like 73213.8 which is correct number.

I need this number to be round up to 73214 and without any decimal.

So I made :

var nf = new Intl.NumberFormat('en-US', {
  maximumFractionDigits:0, 
  minimumFractionDigits:0
});

and changed my code to:

nf.format(shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber)

and it returns 73,214. It did round up my number but also added , how do I remove that decimal?

Jon P
  • 19,442
  • 8
  • 49
  • 72
mafortis
  • 6,750
  • 23
  • 130
  • 288
  • I don't catch why wrapping the whole expression in parentheses changes the final result... – Shidersz Feb 07 '19 at 02:41
  • @Shidersz it's for tax percentage – mafortis Feb 07 '19 at 02:47
  • I can understood the formula, but for me `shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber` and this `(shipingcostnumber * parseInt(tax) / 100 + shipingcostnumber)` should give the same output, not differents ones... – Shidersz Feb 07 '19 at 02:57
  • @Shidersz well i don't know what to say i can't explain it unless share the output which i did, probably is math thing that works with parentheses :) – mafortis Feb 07 '19 at 03:17
  • What do you expect for something like 73213.1? – Jon P Feb 07 '19 at 03:17
  • @JonP no i expected `73214` (round up) and i have it now. – mafortis Feb 07 '19 at 03:18
  • Possible duplicate of [How to round up a number in Javascript?](https://stackoverflow.com/questions/5191088/how-to-round-up-a-number-in-javascript) – Jon P Feb 07 '19 at 03:21

1 Answers1

5

You don't need to write your own function. Just use Math.ceil(n). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil

Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27