1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy
Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Monday's Programming Question

Monday January 5, 2009

I've had a couple of questions about formatting Strings recently so this week's programming question is around the System.out.printf method and the value of Math.PI.

Using the printf method can you create formatted strings that display Math.PI to:

  • match the output of
    System.out.println(Math.PI);
  • only have two decimal places
  • have two decimal places and a positive sign (i.e., "+")
  • make the calculation of 1-Math.PI have two decimal places and a negative sign (i.e,"-")
  • have three leading zeros

To add to the formatting fun can you use the printf method to display the number 1234 as a denary number, a hexadecimal number, an octal number and finally as a denary number with five leading spaces.

Comments
January 20, 2009 at 8:40 am
(1) ixanos says:

This is a completely useles pseydo-article.
And shows that the author is thirsty for some sort of IT-recognition but doesnt have the necessary competences.

January 20, 2009 at 3:20 pm
(2) Paul Leahy says:

Apologies if you find the question unhelpful, but for programmers just starting out formatting a string is useful knowledge. These questions are simply designed to get those who are interested to try out a few Java programming exercises.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.