473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

very simple question, regarding maths in java

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

5 1742
r035198x
13,262 8TB
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
8,658 Expert Mod 8TB
what are the HoursWorked and RateOfPay values, before the calculation?
Nov 7 '09 #3
Ibys
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
8,658 Expert Mod 8TB
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
Ibys
9
thankyou so much! it works fine now :)
Nov 7 '09 #6

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

Similar topics

1
by: LJL | last post by:
Greetings: I have a question regarding the system time and Netscape 7. I am running Win98SE. When Netscape 7 first loads it seems to store the currently set system time zone information. If...
16
by: WindAndWaves | last post by:
Hi there I have $initstartdate = date("d-m-Y"); in my code How can I get it to be date() + 1 or 7 for that matter. Because my server is in the US and I am in New Zealand, they are always...
5
by: Lee David | last post by:
I went to the sun site and downloaded what I hope is the development part of java. I downloaded JDK5 with Netbeans. I installed it and now have a folder in my program group "Netbeans". Is that...
0
by: Patrick.O.Ige | last post by:
I have an sample xml below. I want to do a distinct on the code node and the programDescription. I want the output to be like in a tree structure. So i would like to have PRG004 >Crazy Training...
0
by: Rote Rote | last post by:
I have an sample xml below. I want to do a distinct on the code node and the programDescription. I want the output to be like in a tree structure. So i would like to have PRG004 >Crazy Training...
0
by: Patrick.O.Ige | last post by:
I have a xml file and i want to format it using XSL I needed to do a distinct which is ok on the first node "Code" But when it gets to the "programDescription" node it prints out values for both...
29
by: Knut Olsen-Solberg | last post by:
I try to change the text in a <p> using getElementById(). I wonder what properties exists, and which one to use here. (The following does not work.) Regards Knut ______________________ ...
2
by: rob | last post by:
Hi everyone, Hope someone can give me a clear answer: which way I can go to run asp on apache solaris server? Thanks, Robert
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.