Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2012, 07:14 AM   PM User | #1
mh_and
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
mh_and is an unknown quantity at this point
Math.round question

Hello guys,

A newbie here and newbie in javascript. I'm just learning and really can't do much but read the code, understand and do some changes. Will much appreciate your help here.

So the problem I have is with rounding. Here is the little code I have (there is more and if requested I can post it here, but my question is more general):
Code:
days = calculateDayCount(pickupDate, dropoffDate);
	
	getPrices(days);
	root_days = Math.round(days);
	days_count = root_days;
	$j('#day_count').val(days_count);
	$j('#header_days').html(days_count);
	return ["ok", 0, 0];
days are actually calculated as dropoffDate-pickupDate and I'm getting results like 1.25, 2.89, 3.11 etc etc..

So what the Math.round(days) does, is it rounds as usual and stores in root_days, but the problem is that I need it rounded not as usual. Here is why:
The code will work for a service calculation and if service is provided for more then 2 hours, the service charges for the whole day. Example

1 day 1.5 hours - we'll charge for 1 day
1 day 2.5 hours - we'll charge for 2 days
2 day 0.5 hours - we'll charge for 2 days
2 days 5 hours we'll charge for 3 days
etc etc..

So the question is that how can i round appropriate way to calculate charges properly. I assume 1 hour is appox. 0.04 day.

Thank you guys.. again if you think you need the full code i can post here with the first request.

p.s. I'm thinking that i need to use math.floor here somehow, but can't really think how..
mh_and is offline   Reply With Quote
Old 12-05-2012, 07:51 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
In vanilla Javascript,

Code:
<script type = "text/javascript">

var days = 2.03;  // 2 hours = .0833333 days
var d = parseInt(days)
var fraction = days - d;
if (fraction < .08333333) {
days --;
}
d = Math.ceil(days);
alert ("Charge is for " + d + " days");

</script>
"If you can't explain it simply, you don't understand it well enough”
“Everything should be as simple as it is, but not simpler.”
- both quotes Albert Einstein (German born American Physicist who developed the special and general theories of relativity. Nobel Prize for Physics in 1921. 1879-1955)
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-05-2012, 08:42 AM   PM User | #3
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
And simply ?
Code:
var d = Math.floor(days+11/12);

Last edited by 007julien; 12-05-2012 at 09:08 AM..
007julien is offline   Reply With Quote
The Following 3 Users Say Thank You to 007julien For This Useful Post:
devnull69 (12-05-2012), mh_and (12-05-2012), Philip M (12-05-2012)
Old 12-05-2012, 08:45 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
Code:
var d = parseInt(days)
The JavaScript function for getting the integer portion of a number is Math.floor() so that statement should read:

Code:
var d = Math.floor(days);
parseInt is for converting numbers between bases - eg binary to decimal, hexadecimal to decimal or even base 36 to decimal.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-05-2012, 12:15 PM   PM User | #5
mh_and
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
mh_and is an unknown quantity at this point
@007julien can you please explain what exactly this Math.floor(days+11/12) achieves I'm asking this because...

@felgall & Philip M variables days_count and root_days are used elsewhere in the code also, so I thought i can do this (I know i can assign d to anyting after this, but still didn't want to confuse myself)

Code:
if (days%1<0.11) {root_days = Math.floor(days)} else{
	root_days = Math.floor(days)+1;}
	days_count = root_days;
is the modulus good way of replacing parseInt here? It actually works this way, but when I put days%1<0.09 which is logical to me, it just doesn't work well, skips some hours actually.

Oh and I noticed I can use Math.ceil in place of second math.floor.
mh_and is offline   Reply With Quote
Old 12-05-2012, 12:24 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
11/12 of a day is 22 hours, i.e. 2 hours less than a day. If you add 11/12 to your "days" variable and then cut off the decimals, you'll end up with the correct value

Examples:
Code:
days=2.03
days+11/12 = 2.9466667
Math.floor(days+11/12) = 2 (correct)

days=2.084
days+11/12 = 3,0006667
Math.floor(days+11/12) = 3 (correct, because .084 days is more than 2 hours)
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
mh_and (12-05-2012)
Old 12-05-2012, 12:27 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by mh_and;1297413days;[/CODE

is the modulus good way of replacing parseInt here? .
No. You have been given two scripts - both work, but julien007's is slicker. Don't ask for help and then ignore it.

var d = Math.floor(days+11/12);

11/12 of a day is (24-2) = 22 hours, so if you add 11/12 to the value of days (say 2.09) you get 3.00666666. Math.floor then resolves to 3 (days).
Likewise if days = 2.08 adding 11/12 gives 2.9966666, which resolves to 2 days.

That is what you asked for.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mh_and (12-05-2012)
Old 12-05-2012, 02:31 PM   PM User | #8
mh_and
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
mh_and is an unknown quantity at this point
Quote:
Don't ask for help and then ignore it.
@Philip M Absolutely, I'm just trying to get things done and understand what I'm doing. Also trying to experiment as well..

Now I see how beautifully simple is 007julien's code..

Thank you all..
mh_and is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:31 PM.


Advertisement
Log in to turn off these ads.