|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Rounding issue (ceil)
Hi could you please help me to get this going?
I have a particular rounding issue. I need to determine whether or not a number has 0's as decimal places and depending on that either 'ceil' or not. An example: Code:
pre_tax_t = totaldue_*0.125; // the total of a shopping basket * 12.5% tax tax_t = pre_tax_t.toFixed(3); // this limits the decimal places (3) Now, if the value of tax_t is: 39.131 I'd need to round up (ceil) the third decimal place, which should give something like this: 39.14 but if the value of tax_t is: 20.100 I don't want to 'ceil' and basically leave it as it is because "ceil' would actually return 20.11, which is wrong. Any help is very much appreciated. Cheers Jan |
|
#2
|
|||||
|
|||||
|
Workaround
Hi Jan,
Here is how you can modify the Decimals function so it will return just the effective number of decimals. I mean 1.210 has only two decimals that really count. JavaScript Code:
all the best mecanicu |
|
#3
|
|||
|
|||
|
Quote: Thank you for your help but: When I call that fucntion like this : Code:
test = Decimals(number,decimal places) I get nothing. Something wrong? Cheers Jan |
|
#4
|
|||
|
|||
|
Yes. You are supposed to ask
test = Decimals(number, "."); Some countries use "," as decimal separator so this is the `internationalization` part. The result, in test, will be the number of effective decimals |
|
#5
|
|||
|
|||
|
Quote:
Well, thank you for that. I had to slightly modify that to make it work. Apparently 'number' has to be a STRING. It only works by converting the 'number' to a STRING. The function "toString" is quite handy for that purpose. Code:
pre_tax_t = totaldue_*0.125; // get the tax test = Decimals(pre_tax_t.toString(), '.'); The result, in test, will now be the number of effective decimals. Is it possible to filter the result in test for a 0. Or to put it differently, how can I detect whether there are <3 decimals or whether the third decimal a 0. I'd like to illustrate myissue by means of an example: Given the following figures: totaldue_ -> 313.05 // total of a shopping basket pre_tax_t = totaldue_*0.125; // get the tax that totals to: pre_tax_t -> 39.13125 If the third decimal place is >0, I need to ceil to 2 decimals I can do this with (which is fine): Code:
pre_tax_t -> 39.13125 test_a = Math.ceil( pre_tax_t*100 )/100; // test_a = 39.14 The problem starts with (for example) the following given figures: totaldue_ -> 160.80 pre_tax_t = totaldue_*0.125; that totals to: pre_tax_t -> 20.1 If there is no third decimal or if the third decimal is a 0, I need to rather do nothing or floor, because with 'ceil I'd get this (which is wrong: Code:
pre_tax_t -> 20.1 test_a = Math.ceil( pre_tax_t*100 )/100; // test_a = 20.11 This would be ok: Code:
pre_tax_t -> 20.1 test_b = Math.floor( pre_tax_t*100 )/100 20.1 Can I somehow use your code to get this going? Cheers Jan |
|
#6
|
|||
|
|||
|
Fixed
Thank you for your help.
That issue has been fixed with some thought-provoking impulse from you guys. Well here ist the code, certainly a bit different because I use that javascript part as a php-embedded one. Please have a closer look at Line 8, because thats the juicy part. To floor the result in Line 12 in order to get the right result is actually a bit dowdy, but I can live with it. Code:
Line 1 echo "pre_tax_t = totaldue_*0.125;\n"; // get the amount tax Line 2 echo "tax_t = pre_tax_t.toFixed(3);\n"; // fix to 3 decimals and use for ceil Line 3 echo "tax_l = pre_tax_t.toFixed(2);\n"; // fix to 2 decimals and do nothing Line 4 echo "tk = tax_t*100;\n"; Line 5 echo "tk = tk.toFixed(2);\n"; Line 6 echo "tfl = Math.floor( tax_t*100 );\n"; Line 7 echo "tfl = tfl.toFixed(2);\n"; Line 8 echo "tax_t = (tk != tfl) ? (Math.ceil( tax_t*100 ) ) / 100 : tax_l;\n";// Line 3 Line 9 echo "pre_tax_ts = ship_cost_*0.125;\n"; Line 10 echo "tax_s = pre_tax_ts.toFixed(3);\n"; Line 11 echo "tax_s = Math.ceil(tax_s*100 )/100;\n"; Line 12 echo "tax = ((Math.floor(tax_t*100))/100)+((Math.floor(tax_s*100))/100);\n"; Line 13 echo "totaldue_ = totaldue_+ship_cost_+tax;\n"; Cheers Jan |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Rounding issue (ceil) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|