Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

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

Wed Sep 15, 2010 3:10 pm

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

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:
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. 8)

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
 
SNM
Emperor Gerbilius I
Posts: 6209
Joined: Fri Dec 30, 2005 10:37 am

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

Wed Sep 15, 2010 3:24 pm

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
 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

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

Wed Sep 15, 2010 3:37 pm

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
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

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

Wed Sep 15, 2010 3:41 pm

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.
 
Corrado
Minister of Gerbil Affairs
Posts: 2574
Joined: Sun Feb 17, 2002 7:00 pm

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

Wed Sep 15, 2010 3:50 pm

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.
 
dextrous
Gerbil Elite
Posts: 570
Joined: Mon Nov 22, 2004 1:49 pm
Location: Ooooooooooklahoma

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

Wed Sep 15, 2010 4:42 pm

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
 
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)

Wed Sep 15, 2010 5:49 pm

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):













// corrado's method
double d = 16.66;
int i = (int)(d + 0.5);














// 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 work
if(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
 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

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

Wed Sep 15, 2010 6:05 pm

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
 
Corrado
Minister of Gerbil Affairs
Posts: 2574
Joined: Sun Feb 17, 2002 7:00 pm

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

Wed Sep 15, 2010 6:10 pm

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
 
wibeasley
Gerbil Elite
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)

Wed Sep 15, 2010 6:13 pm

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.
 
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)

Wed Sep 15, 2010 6:24 pm

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
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

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

Wed Sep 15, 2010 7:51 pm

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.
 
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)

Wed Sep 15, 2010 9:00 pm

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. :oops: . 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... :P
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

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On