|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Math.ceil depending on the number of decimal places
Hi,
I've got an issue and I hope you can help me to get this going. Well, to start with, we all know what ceil does: ceil(x) - Rounds up the value of "a" to the next integer. If the value is a already whole number, the return value will be the same as the passed value. Code: The example returns 13. resultval = Math.ceil(12.01) ------------------ The issue: In particular this is about a tax rate of 12.5% whereas totaldue_ is the total of a given shopping basket and ship_cost_ is a given figure, say 7.91: Code:
tax_t = totaldue_*0.125; tax_s = ship_cost_*0.125; Now, if I for example would need to only ceil decimal places (2) I'd do something like that. Code:
tax = ((Math.ceil(tax_t*100))/100)+((Math.ceil(tax_s*100))/100); That gives me the ceiled total totaldue_ = totaldue_+ship_cost_+tax; So far everything is fine and everything works as expected as long as the sum of tax_t = totaldue_*0.125; and/or tax_s = ship_cost_*0.125; has more that 2 decimal places, for example: Code:
tax_t = 160.81*0.125 this totals = 20.10125 tax_s = 7.91*0.125 this totals = 0.98875 The following causes the problem for me: Code:
tax_t = 160.80*0.125 this totals = 20.1 tax_s = 7.91*0.125 this totals = 0.98875 Can you see the difference? In the event that I get a number with less than 3 decimal places I don't want to ceil. Only in that particular case I rather need to floor or do nothing. So I guess I'd need something to determine the number of decimal places against the result of that math. How could I realise that? Cheers Jan |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Math.ceil depending on the number of decimal places |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|