• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Commercial Rounds

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic