• Post Reply Bookmark Topic Watch Topic
  • New Topic

Commercial Rounds  RSS feed

 
Radost Petrova
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi This semester I started learning Informatics as a subject and I am an absolute beginner. We have to do homework every week and this time I couldn't figure out, how eactly the task should be solved.

And the task is we have to round some numbers. If the number ist for example a double 3.14 or 0.49 - we have to round it down to an int 3 and an int 0, and if we have a number like 10.5 it should be rounded up to int 11.

We were told NOT to use the Math.round method for this task and were also given the hint, that the task could be done in only one line of code.

Exactly the creating if the one line of code is the thing I cannot figure out. Does someone of you guys know an efficient solution to this problem? How can I convert both types of doubles in one and the same command?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it for a moment. If you simply truncate the number as is to an integer (there's a method for that), the .5 and over numbers would not end up with the right value. So.... is there something you could do to the number that would cause the truncation to give the right value?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radost Petrova wrote:
We were told NOT to use the Math.round method for this task and were also given the hint, that the task could be done in only one line of code.


Yea, that hint isn't really a hint is it? Hint. What is the difference between an integer and a double? And what happens when you cast between them?

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!