JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 17th, 2007, 10:49 PM
phpscripter phpscripter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 phpscripter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 53 m 38 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old April 18th, 2007, 02:40 AM
mecanicu mecanicu is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 112 mecanicu User rank is Private First Class (20 - 50 Reputation Level)mecanicu User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 23 m 6 sec
Reputation Power: 3
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:
Original - JavaScript Code
  1. function Decimals(x, dec_sep)
  2. {
  3.     var tmp=new String();
  4.     tmp=x;
  5.     if (tmp.indexOf(dec_sep)>-1)
  6.     {
  7.         len = tmp.length;
  8.         ret_val = len-tmp.indexOf(dec_sep)-1;
  9.         while (tmp.charAt(--len)=="0")
  10.             ret_val--;
  11.     }
  12.     else
  13.         ret_val = 0;
  14.     return ret_val;
  15. }

all the best
mecanicu

Reply With Quote
  #3  
Old April 18th, 2007, 03:06 AM
phpscripter phpscripter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 phpscripter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 53 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by mecanicu
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:
Original - JavaScript Code
  1. function Decimals(x, dec_sep)
  2. {
  3.     var tmp=new String();
  4.     tmp=x;
  5.     if (tmp.indexOf(dec_sep)>-1)
  6.     {
  7.         len = tmp.length;
  8.         ret_val = len-tmp.indexOf(dec_sep)-1;
  9.         while (tmp.charAt(--len)=="0")
  10.             ret_val--;
  11.     }
  12.     else
  13.         ret_val = 0;
  14.     return ret_val;
  15. }

all the best
mecanicu


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

Reply With Quote
  #4  
Old April 18th, 2007, 04:47 AM
mecanicu mecanicu is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 112 mecanicu User rank is Private First Class (20 - 50 Reputation Level)mecanicu User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 23 m 6 sec
Reputation Power: 3
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

Reply With Quote
  #5  
Old April 18th, 2007, 09:44 PM
phpscripter phpscripter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 phpscripter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 53 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by mecanicu
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


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

Reply With Quote
  #6  
Old April 19th, 2007, 01:00 AM
phpscripter phpscripter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 phpscripter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 53 m 38 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Rounding issue (ceil)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT