Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

crazykid888

macrumors newbie
Original poster
Aug 4, 2010
25
0
hey there,
im writting a simple program for school that takes input of temperature and dewpoint and converts it to the humidex.

My program works except i still need to round my humidex number off.
I've tried inserting Math.round but i always get an error.
Im wondering how do i use this method properly for it to work! (note, i only need one decimal space)

Here is a snippet of my code:

humidex = + temp + + h ;

i need the humidex to be rounded to one decimal. where do i put Math.round?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
Here is a snippet of my code:

humidex = + temp + + h ;

Don't post snippets. Post complete sections of code, so someone else can compile and run them.

What do you intend that snippet to do? Without seeing the context of the calculation, it's impossible to tell the intent.

If you have code that gives an error when compiled, then post the code that gives the error. Saying "I get an error" without showing the code is useless. We can't see your screen. We can't read your mind. We have no idea what code you wrote that doesn't work, until you post the actual code.

EDIT:
As given, the snippet is equivalent to this:
Code:
 humidex = temp + h ;
Two of the +'s are completely unnecessary and have no effect. Which leads to the question: Do you think they're necessary? What do you think the extra +'s do?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
If that code compiles, I am very surprised. Math.round returns the closest int when a float is passed in, and the closest long when a double is passed in. A common method to round to a decimal place using something like this is to shift using multiplication by a power of ten, then apply the round, then divide by the power of ten. If you want n places, you use 10^n. In this case, that's 10^1, which is 10.

As chown33 said, a complete example plus any errors and warnings will help a lot, as well as any definitions that one needs. Humidex is not a term I know.

We are happy to help, but we need something to work with.

-Lee

Edit: looked it up, there's a unary + in java, but it has no meaning here.
 

crazykid888

macrumors newbie
Original poster
Aug 4, 2010
25
0
Sorry guys, im knew to java
I understand it pretty well for just starting with it but i dont see why this Math.round doesnt work! lol :p

Two of the +'s are completely unnecessary and have no effect. Which leads to the question: Do you think they're necessary? What do you think the extra +'s do?

Ill post all my code. We need to use the + to append a variable . without it you cant add it in and it generate an error.




Thanks for the quick response!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The binary + will add two values. Unary (single argument) + does something else. I don't know what, to be honest, and chown33 was asking if you do. I don't know if unary + applied to capital D double is going to do something odd, but you don't need it. Using capital D double is odd here in general as you don't need to put it in a collection or do anything else that requires an Object. All of the auto-boxing/unboxing is costly and unnecessary. I'd try w/ lowercase d double, and without the extraneous unary +s.

-Lee
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
Well that's certainly some crazy code.

Lee1210 is right: change all the Double types to double and things will work better. I tried it and it works.

I don't know how or where you learned to code that way, but it's about as wrong as it could possibly be, while still managing to somewhat work. The key word there is "somewhat", because there a whole lot of cases where this approach simply won't work, or it requires some extra conversions to be performed. I'd like to know if this practice is being taught by someone, or whether you just stumbled across it in your own explorations.

The Double type is actually an Object type, and when it's used in arithmetic, it has to be converted back to its corresponding primitive type (double). In Java, the unary + happens to perform that function, by forcing the Double object to be "unboxed" to a double, which is the actual primitive type.

The reason Math.round() doesn't work is because Math.round() returns a long type, which cannot be assigned to a Double type. The error message is telling you this. It found a long type, but it expects a java.lang.Double, which is the full name of the Double type. Ironically, if you use the double type, then Math.round() will compile, because a long is assignable to a double.

Boxing and unboxing is actually a fairly complicated subject, even though it shouldn't be. It's complicated because the compiler isn't always able to figure out what it should do without specific "hints" in the source code, like unary + or certain other conversions.

I suggest restudying the primitive Java types, all of whose names begin with lower-case letters: int, long, double, float, etc. I also suggest discussing this your teacher, by showing him or her your code using Double, and asking for an explanation of why Math.round() doesn't compile.


EDIT:
A completely separate question is whether Math.round() is the thing to use in this case. You said you needed "one decimal space", which I interpret as one digit after the decimal point, such as "31.6" or "45.2".

If you look at what Math.round() does, and you should do so, it always rounds to the nearest integer value. That means it will return 32 if passed 31.6, which means it's not directly suitable for getting what you want.

However, it can be used as a building block to get what you want. Lee already gave an explanation, so try understanding that. If his explanation doesn't make sense, then post again.
 

crazykid888

macrumors newbie
Original poster
Aug 4, 2010
25
0
wow thanks.
I never knew the D would make a difference. Ill try this out and see how it works.
The reason my code is probably really bad is because ive never taken a programming courses until this year, coz my first year eng program requires it.

So Double is an object type and double is the data type right?
My silly lab instructor told me today that double"" was supposed to be Double always.
So i was a little confused because we learn it as double.
And maybe the reason why its so weird is because we were told to use a scanner instead of JOptionPane for user input.
Things Ive learned so far in this class have been the basics and some parsing and we've just moved into casting.


oh and,
A completely separate question is whether Math.round() is the thing to use in this case.

Our prof wanted us to use Math.round()
Thanks a lot for the help. Ill get back if it works or not.

All of the auto-boxing/unboxing is costly and unnecessary. ""... and without the extraneous unary +s.

-Lee

what do you mean by these two things?
I understood everything else
and yes I'm the biggest newbie to java :p
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
wow thanks.
I never knew the D would make a difference. Ill try this out and see how it works.
The reason my code is probably really bad is because ive never taken a programming courses until this year, coz my first year eng program requires it.
I didn't expect you to know the D would make a difference. On the whole, your code is fine, except for the use of Double instead of double.

So Double is an object type and double is the data type right?
Yes, that's right, although I don't expect you to fully understand the difference at this point. That's OK because I wouldn't expect it from any beginner.

My silly lab instructor told me today that double"" was supposed to be Double always.
So i was a little confused because we learn it as double.
Your lab instructor is wrong. When you learned it as double, that was the correct way.

Thanks a lot for the help. Ill get back if it works or not.
Glad to help, and do let us know how it works out. Or if it doesn't work, feel free to post again.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
wow thanks.
I never knew the D would make a difference. Ill try this out and see how it works.
The reason my code is probably really bad is because ive never taken a programming courses until this year, coz my first year eng program requires it.

So Double is an object type and double is the data type right?
My silly lab instructor told me today that double"" was supposed to be Double always.
So i was a little confused because we learn it as double.

Thanks a lot for the help. Ill get back if it works or not.

Your lab instructor is wrong. You might not be in a position to tell them so, but if you don't need Objects, always use primitives. They are smaller, faster, and behave in a more straight-forward way. It's neat that Java has autoboxing/unboxing, it saves code. There are times it will bite you, though, and it is slow.

The primitive types all start with a lower case letter. Call them primitives, not data types, as data type is very broad. Each primitive has an Object wrapper with a capital letter that can be used when you need to place the value in a collection or pass it somewhere as an Object.

Everyone starts somewhere, but the problem with starting up in the clouds with Java instead of something simpler is you are almost immediately forced to write code you don't understand. Stick with it, but try to read every line of your code and explain out loud what it's doing. If there's something that you don't understand, write it down and ask your professor, or ask here. Maybe ask your lab instructor, but they have lost my trust. Maybe they meant something else and failed to thoroughly explain themselves.

-Lee
 

crazykid888

macrumors newbie
Original poster
Aug 4, 2010
25
0
ohone question actually
how do i get it to round to one decimal.
my answer without the rounding is 10.65...... a bunch of numbers
I want it to round at 10.7

right now using Math.round its rounding it off to 11.0

how do i fix this?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You need to adjust by 10 as I explained above.
Take your value, multiply by 10, use Math.round, divide by 10.
Code:
double toRound = 45.23;
double roundedValue = Math.round(toRound * 10.)/10.;
roundedValue should be 45.2.
Computers do math in binary, though, so sometimes these things aren't exact, but with numbers of this magnitude it should be fine.

Earlier you asked what I meant by autoboxing and unboxing, and unary +. I didn't see that part of your post for whatever reason.

Boxing/unboxing is the process of wrapping a primitive (double) in it's Object wrapper (Double), and taking it back out. Java does this for you automatically in most cases, and this is referred to as autoboxing and autounboxing. The compiler sees that you've used a Double where a double is required, and it adds the code for you to get the double out of the Double. Also, when a Double is needed for, say, an ArrayList and you use a double, the compiler will create a new Double Object to use. This subject is a little involved, and it's probably early in your programming career to have to deal with it. Use primitives until someone says otherwise.

As for unary +:
+ temp + + h
This expression has two unary +s. This expression can be broken down like:
(+temp) + (+h)
The middle + is a binary + that adds two operands. The other two are over-verbosely saying that the expressions temp and h are "positive". This doesn't mean much, but this is a parallel to unary -, which is like multiplying by -1. Unary means only one operand. Other unary operators are:
! for not
++ both prefix and postfix increment
-- both prefix and postfix decrement

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.