Jump to content

Need some help with very basic Java.

Cacao
Go to solution Solved by Student,

i'd do it like this

System.out.println(((double) Math.round(3.14 * 10)) / 10);

Hey guys, so here's what I have to do:

 

Write a program that will round a value in a double value to one tenth, eg. the value 3.14 is rounded to 3.1

 

Can anyone help me out with this? Do I need to convert the double into some other data type in order to round it?

 

92f884372ed2b981b8bd5472d70552a9.png

 

Obviously this will just output 3.14, I'm assuming I need to something like --->  var1 = (IDK WHAT TO PUT HERE) 3.14;

 

Thanks!

Setup Video -----------Peasant Crushing Specs----------- 4K Benchmarks


-CPU- i7 3930k @4.8GHz 1.4v -Mobo- Asus Rampage IV Extreme -GPUs- 2x GTX Titan Hydrocopper SLI -RAM- 32GB (8x4GB) Corsair Vengeance 1600MHz -Storage- 500GB Samsung 840 SSD | 2TB WD Green HDD


-Monitors- 3x BenQ XL2420T | 1x Dell U2713HM -Mouse- Steelseries Rival -Keyboard- Corsair K70 Cherry MX Brown -Headphones- Audio Techinca ATH-M50 -Microphone- RØDE NT1-A

Link to comment
Share on other sites

Link to post
Share on other sites

There sure seems to be a lot of talk on this forum about how people seem to understand what goes into code and such...

Not seeing any of it at work here xD

Leave it to the kid making music, then.

 

The best I can think of is something like;

double var1 = 3.14;
System.out.println(var1); // var1 is 3.14

int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(var1);

// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
var1 = bd.doubleValue();

System.out.println(var1); // var1 is 3.1

/qb

I apologize if this is just an abomination of crap, I have touched java in a couple years, since running RuneScape private servers xD

I really wish that we'd just make organic hardware already, that grows and adapts to the demands it needs to meet. That way, grannies' computers can be floppy sacks of organicness and the 12 year old Minecrafters will look like the guys that only do bicep curls, and the nerdy programmers will finally have justice, with their body-builder rigs that skipped leg day.


CPU: i7-4770k 4.8GHz | Motherboard: Asus Maximus Hero | RAM: 16gigs 2133MHz | GPU: SLI Gigabyte OC 2gb 770's | Case: INWIN GRone | Storage: 1tb Blue, 60gb SSD | PSU: Silencer MK II 950w | Cooling: Modded H100i

Link to comment
Share on other sites

Link to post
Share on other sites

There sure seems to be a lot of talk on this forum about how people seem to understand what goes into code and such...

Not seeing any of it at work here xD

Leave it to the kid making music, then.

 

The best I can think of is something like;

double var1 = 3.14;

System.out.println(var1); // var1 is 3.14

int decimalPlaces = 2;

BigDecimal bd = new BigDecimal(var1);

// setScale is immutable

bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);

var1 = bd.doubleValue();

System.out.println(var1); // var1 is 3.1

/qb

I apologize if this is just an abomination of crap, I have touched java in a couple years, since running RuneScape private servers xD

Thanks for your reply, but that's quite a bit more advanced than me :P I think the solution is much easier. 

 

Plus your program just gives me a bunch of errors when I try to compile it D:

Setup Video -----------Peasant Crushing Specs----------- 4K Benchmarks


-CPU- i7 3930k @4.8GHz 1.4v -Mobo- Asus Rampage IV Extreme -GPUs- 2x GTX Titan Hydrocopper SLI -RAM- 32GB (8x4GB) Corsair Vengeance 1600MHz -Storage- 500GB Samsung 840 SSD | 2TB WD Green HDD


-Monitors- 3x BenQ XL2420T | 1x Dell U2713HM -Mouse- Steelseries Rival -Keyboard- Corsair K70 Cherry MX Brown -Headphones- Audio Techinca ATH-M50 -Microphone- RØDE NT1-A

Link to comment
Share on other sites

Link to post
Share on other sites

i'd do it like this

System.out.println(((double) Math.round(3.14 * 10)) / 10);

i am not a native speaker of the english language

[spoiler=My Rig: ]CPU: i7-3770k@Stock | Ram: 3x4GB@1600Mhz | Graka: 660TI@Stock | Storage: 250GB 840Evo, 1x1TB,2x2TB,2x640GB,1x500GB (JBOD) + NAS: DLINK DNS-320 2x3TB Raid1

 
Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for your reply, but that's quite a bit more advanced than me :P I think the solution is much easier. 

 

Plus your program just gives me a bunch of errors when I try to compile it D:

Ohhhhhhhhhhhhhhh! You're doing the think from school, aren't you! Dude, had the same worksheet in my game design class that I was in for a few weeks last year.

Let me try a couple things. Again, no promises, but I may come up with something. It's cool to work with Java again :P

I really wish that we'd just make organic hardware already, that grows and adapts to the demands it needs to meet. That way, grannies' computers can be floppy sacks of organicness and the 12 year old Minecrafters will look like the guys that only do bicep curls, and the nerdy programmers will finally have justice, with their body-builder rigs that skipped leg day.


CPU: i7-4770k 4.8GHz | Motherboard: Asus Maximus Hero | RAM: 16gigs 2133MHz | GPU: SLI Gigabyte OC 2gb 770's | Case: INWIN GRone | Storage: 1tb Blue, 60gb SSD | PSU: Silencer MK II 950w | Cooling: Modded H100i

Link to comment
Share on other sites

Link to post
Share on other sites

 

i'd do it like this

System.out.println(((double) Math.round(3.14 * 10)) / 10);

It could very well be that simplistic.

Dhuurrr on my part.

I really wish that we'd just make organic hardware already, that grows and adapts to the demands it needs to meet. That way, grannies' computers can be floppy sacks of organicness and the 12 year old Minecrafters will look like the guys that only do bicep curls, and the nerdy programmers will finally have justice, with their body-builder rigs that skipped leg day.


CPU: i7-4770k 4.8GHz | Motherboard: Asus Maximus Hero | RAM: 16gigs 2133MHz | GPU: SLI Gigabyte OC 2gb 770's | Case: INWIN GRone | Storage: 1tb Blue, 60gb SSD | PSU: Silencer MK II 950w | Cooling: Modded H100i

Link to comment
Share on other sites

Link to post
Share on other sites

It could very well be that simplistic.

Dhuurrr on my part.

 

 

i'd do it like this

System.out.println(((double) Math.round(3.14 * 10)) / 10);

Thanks man, that totally worked! :D

Setup Video -----------Peasant Crushing Specs----------- 4K Benchmarks


-CPU- i7 3930k @4.8GHz 1.4v -Mobo- Asus Rampage IV Extreme -GPUs- 2x GTX Titan Hydrocopper SLI -RAM- 32GB (8x4GB) Corsair Vengeance 1600MHz -Storage- 500GB Samsung 840 SSD | 2TB WD Green HDD


-Monitors- 3x BenQ XL2420T | 1x Dell U2713HM -Mouse- Steelseries Rival -Keyboard- Corsair K70 Cherry MX Brown -Headphones- Audio Techinca ATH-M50 -Microphone- RØDE NT1-A

Link to comment
Share on other sites

Link to post
Share on other sites

Ohhhhhhhhhhhhhhh! You're doing the think from school, aren't you! Dude, had the same worksheet in my game design class that I was in for a few weeks last year.

Let me try a couple things. Again, no promises, but I may come up with something. It's cool to work with Java again :P

I'm doing a Grade 12 Computer Programming course with the Canadian Online Highschool ;)

Setup Video -----------Peasant Crushing Specs----------- 4K Benchmarks


-CPU- i7 3930k @4.8GHz 1.4v -Mobo- Asus Rampage IV Extreme -GPUs- 2x GTX Titan Hydrocopper SLI -RAM- 32GB (8x4GB) Corsair Vengeance 1600MHz -Storage- 500GB Samsung 840 SSD | 2TB WD Green HDD


-Monitors- 3x BenQ XL2420T | 1x Dell U2713HM -Mouse- Steelseries Rival -Keyboard- Corsair K70 Cherry MX Brown -Headphones- Audio Techinca ATH-M50 -Microphone- RØDE NT1-A

Link to comment
Share on other sites

Link to post
Share on other sites

I know it's been answered, but if your sole purpose is to print this rounded double, this will suffice.

 

double var = 3.14;System.out.printf("%.1f\n", var);

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×