• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Printing a Double number as a Whole number

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again.

I have a question guys.

I'm asked to print 123.9876 as a 2 decimal number by using System.out.printf statement. I did this one.
I'm also asked to print this number as a whole number in System.out.printf statement. This is where i got stuck.

Any suggestions?

This is how i printed it in 2 decimals.

 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.printf("%6.0d%n", 123.4567890); ?

That will round to 123; you can also cast to an int, which truncates, so this prints 999:-
System.out.println((int)999.99999999999);
 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how many decimal places is a whole number?

And there you have it.
 
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what would be 123.9876 as a whole number? 123 or 124 or 1239876 ?

Have you been taught about cast operator?
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:And what would be 123.9876 as a whole number? 123 or 124 or 1239876 ?

Have you been taught about cast operator?



it doesn't say that but i assume 1239876
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:System.out.printf("%6.0d%n", 123.4567890); ?

That will round to 123; you can also cast to an int, which truncates, so this prints 999:-
System.out.println((int)999.99999999999);



it didn't work by the way.It gives erors.
 
Liutauras Vilda
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stop guessing, define first what is the whole number first. Read instructions carefully, what is written in there. Do you need to round up to the next whole number or do you need to truncate everything after the radix point, or you need to multiply the number by number x in order to get whole number.

double number = 10.5;

a) number x 10 // 105
b) (int)number // 10
c) Math.round(number) // 11

You have to decide what you need here.
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Stop guessing, define first what is the whole number first. Read instructions carefully, what is written in there. Do you need to round up to the next whole number or do you need to truncate everything after the radix point, or you need to multiply the number by number x in order to get whole number.

double number = 10.5;

a) number x 10 // 105
b) (int)number // 10
c) Math.round(number) // 11

You have to decide what you need here.



Since i don't have any special instructions i have to guess.
Here is Question.

a: Write a System.out.printf...statement to print whole number.

b. Write a System.out.printf...statement to print a 2 decimal number.

That's all i have.
 
Liutauras Vilda
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serkan Kilic wrote:Since i don't have any special instructions i have to guess.
Here is Question.

a: Write a System.out.printf...statement to print whole number.

b. Write a System.out.printf...statement to print a 2 decimal number.

That's all i have.


You changed the requirements. In your initial post you mentioned exact number you need to print it as a whole. But from what I understand from your latest post, it seems you're going to be tested on understanding printf statement structure only.

Start by reading here (<- link).
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Serkan Kilic wrote:Since i don't have any special instructions i have to guess.
Here is Question.

a: Write a System.out.printf...statement to print whole number.

b. Write a System.out.printf...statement to print a 2 decimal number.

That's all i have.


You changed the requirements. In your initial post you mentioned exact number you need to print it as a whole. But from what I understand from your latest post, it seems you're going to be tested on understanding printf statement structure only.

Start by reading here (<- link).



I didn't change anything.
Number is same.

Double number = 123.9876;
 
Liutauras Vilda
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the link I gave you. You'll find answer there. Personally myself wouldn't be able to explain it better with such a nice examples.
 
Les Morgan
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serkan,

I can only assume that your instructor is wanting the same thing that mine wanted when he gave a similar assignment. We were covering floats and he gave us a number and said to print it out as listed, then with 2 digits, then as a whole number. What he was looking for was what happens in the printf statement to the number. In my case it was in "C", but in your case it is in Java.

Serkan Kilic wrote:

Liutauras Vilda wrote:

Serkan Kilic wrote:Since i don't have any special instructions i have to guess.
Here is Question.

a: Write a System.out.printf...statement to print whole number.

b. Write a System.out.printf...statement to print a 2 decimal number.

That's all i have.


You changed the requirements. In your initial post you mentioned exact number you need to print it as a whole. But from what I understand from your latest post, it seems you're going to be tested on understanding printf statement structure only.

Start by reading here (<- link).



I didn't change anything.
Number is same.

Double number = 123.9876;

 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone!
 
All of the following truths are shameless lies. But what about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic