By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

very simple question, regarding maths in java

P: 9
Hi, i am just starting to learn javascript, so i am probably doing something very simple wrong. i have read a lot of articles on maths in java, but cant find anything simple enough for my problem. I am looking for how to get an IF statement to give me the correct output. I am trying to get it to give me a result for a weeks pay, taking that the code worked before i tried to add the IF statement, to make it that if HoursWorked >= 20, the rate of pay would be an extra $2 p/hr. i had the following code to make this work

Expand|Select|Wrap|Line Numbers
  1. if (HoursWorked >= 20)
  2. {
  3.   WeeksPay = parseFloat(HoursWorked) * parseFloat(RateOfPay + 2);
  4. }
  5. else
  6. {
  7.   WeeksPay = parseFloat(HoursWorked) * parseFloat(RateOfPay);
  8.  
but, as simple maths will tell you, the answer is not $3800!! can anyone please tel me what silly mistake i am making here please :) sorry to be asking something so simple, but my teacher is away for the weekend and i have just started this module in my course.
Nov 6 '09 #1

✓ answered by Dormilich

that explains itÖ
Expand|Select|Wrap|Line Numbers
  1. parseFloat(RateOfPay + 2)
the + operator does not only adds numbers, it also concatenates strings. and thatís whatís happening.

better use
Expand|Select|Wrap|Line Numbers
  1. parseFloat(RateOfPay) + 2

Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
Java != Javascript.
I'll assume you meant what you said when you said javascript so I'll move this to the Javascript forum.
Nov 6 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
what are the HoursWorked and RateOfPay values, before the calculation?
Nov 7 '09 #3

P: 9
Expand|Select|Wrap|Line Numbers
  1. HoursWorked=prompt("How many hours did you work this week?","25");
  2. RateOfPay=prompt("How much are you paid per Hour?","15")
i dont even know how it is getting the result it is, 3800... all the other tasks i have been doing have been working fine, even the more complicated ones.
Nov 7 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
that explains itÖ
Expand|Select|Wrap|Line Numbers
  1. parseFloat(RateOfPay + 2)
the + operator does not only adds numbers, it also concatenates strings. and thatís whatís happening.

better use
Expand|Select|Wrap|Line Numbers
  1. parseFloat(RateOfPay) + 2
Nov 7 '09 #5

P: 9
thankyou so much! it works fine now :)
Nov 7 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.