## Need to Round up Double and Convert back to Integer (JAVA)

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

### Need to Round up Double and Convert back to Integer (JAVA)

Today my teacher was being a complete a\$\$hole and wouldn't tell me how to round up a double variable into a integer.

(using java btw)

Basically I was calculating the percentage of expense by taking the rent(for instance) and divided it by the total revenue. They were all declared as integers to start.

Ex. rent = 250
revenue = 1500

Code: Select all
`rentExpense = (rent / revenue) * 100`

That was the problem I was trying to complete. I started by using (double) and (int) in front of the numbers to covert them but they came out as 16 in the end(or 16.66). But my teacher wanted it round to 17 (as an integer).

So I would try stuff like this:
Code: Select all
`rentExpense = (int)((double)(rent / revenue) * 100);`

I know she wants me to have problem solving skills but I was trying to figure it out for 1.5 hours. It's not just going to come to me.

Help a bro out?
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP

Posts: 440
Joined: Thu May 22, 2008 4:33 pm

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Have you tried googling for round? Or else figure out how to do it on your own; it's a pretty simple math problem if you know the truncation rules...
Core i7 920, 3x2GB Corsair DDR3 1600, 80GB X25-M, 1TB WD Caviar Black, MSI X58 Pro-E, Radeon 4890, Cooler Master iGreen 600, Antec P183, opticals
SNM
Emperor Gerbilius I

Posts: 6206
Joined: Fri Dec 30, 2005 10:37 am

### Re: Need to Round up Double and Convert back to Integer (JAVA)

SNM wrote:Have you tried googling for round? Or else figure out how to do it on your own; it's a pretty simple math problem if you know the truncation rules...

That the kind of attitude my teacher was giving me

I am just looking for an answer. I learn better when I know what I am doing.
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP

Posts: 440
Joined: Thu May 22, 2008 4:33 pm

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Forget all the messing around with floating point types, do this all as integer math.

You already have the first part done when you are getting 16, now you need to do a bit extra to decide if that is OK (rounded down) or if you need to add 1 for rounding up. Like SNM I'm not going to actually do your work for you but the big hint I'll give you is to look at the modulus operation.
notfred
Grand Gerbil Poohbah

Posts: 3736
Joined: Tue Aug 10, 2004 10:10 am

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Couldn't tell you the syntax in java, but basically, you add .5 to whatever the number is, and then lop it down to an integer.

14.2 + .5 = 14.7, INT'ed to 14.
14.6 + .5 = 15.1, INT'ed to 15.
Having twins has dramatically reduced the time I have to nerd out.
Minister of Gerbil Affairs

Posts: 2568
Joined: Sun Feb 17, 2002 7:00 pm
Location: Gilbertsville, PA

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Math.Round() anybody?
"I take sibling rivalry to the whole next level, if it doesn't require minor sugery or atleast a trip to the ER, you don't love her." - pete_roth
"Yeah, I see why you'd want a good gas whacker then." - VRock
dextrous
Gerbil Elite

Posts: 563
Joined: Mon Nov 22, 2004 1:49 pm
Location: Ooooooooooklahoma

### Re: Need to Round up Double and Convert back to Integer (JAVA)

As dextrous suggests, I would personally use the library functions (for clearer/easier to read code), but I suspect your teacher would frown upon that method. Corrado's method is probably the quickest and shortest way to do it. notfred's method can also be accomplished using a subtract operation. I'll post the code, but you'll have to scroll down inside the box to see it (don't look until you've given up [again] after trying their suggestions):

Code: Select all
`// corrado's methoddouble d = 16.66;int i = (int)(d + 0.5);`

Code: Select all
`// notfred's method (modified)double d = 16.66;int i = (int)d;// Since we can guarantee the difference between d and i to be less than 1, we only need to check// if the difference is greater than or equal to .5. If it is, then it needs to be rounded up; so, do so.// Otherwise, the stored value is correct// You can also use a modulus instead of a subtract operation and it will still workif(d - i >= 0.5){     i++;}`
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m
mortifiedPenguin
Gerbil Elite

Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm

### Re: Need to Round up Double and Convert back to Integer (JAVA)

The way the program works it that the user inputs the expense amounts. So I can't just add to it. Or can I?
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP

Posts: 440
Joined: Thu May 22, 2008 4:33 pm

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Sure you can. You have the user input into a variable, so just do like varX = varX + .5 to keep the same variable or varY = varX + .5
Having twins has dramatically reduced the time I have to nerd out.
Minister of Gerbil Affairs

Posts: 2568
Joined: Sun Feb 17, 2002 7:00 pm
Location: Gilbertsville, PA

### Re: Need to Round up Double and Convert back to Integer (JAVA)

What code have you written have so far? Taking a string input and converting it into a number should be in the first chapter of your textbook.
wibeasley
Gerbil Elite
Gold subscriber

Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Or, if you want to hold onto the original value to use for something else (i.e. use in another operation), you can copy it into a temporary variable then add to it.
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m
mortifiedPenguin
Gerbil Elite

Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm

### Re: Need to Round up Double and Convert back to Integer (JAVA)

mortifiedPenguin - that's not really the method I was suggesting as you are still using floating point types. I know the language here is Java, but my method works regardless of language even on embedded systems that don't support floating point types.
notfred
Grand Gerbil Poohbah

Posts: 3736
Joined: Tue Aug 10, 2004 10:10 am

### Re: Need to Round up Double and Convert back to Integer (JAVA)

Fair enough. I just realized my mistake was assuming they were floats to begin with, which interfered with the interpretation of your post. Rereading the OP indicates they started as ints,. Whoops. . It actually took me a moment to figure out the method you suggested (and the use of a C compiler), and I actually find it quite elegant; especially in the light that it can be used on non floating-point systems. I will have to remember this one.

I think, this time, I will pass on posting the code...
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m
mortifiedPenguin
Gerbil Elite

Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm