This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Format number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Format number" Watch "Format number" New topic

Format number

Tejal Modi

Joined: May 04, 2000
Posts: 3
I am new to javaranch and java also. I find it very useful for clearing any doubts.I wanted to know equivalent of printf of C in java. how to convert 123.245 into 123.25?

Joined: Aug 06, 2000
Posts: 9
in java i dont think u got any function for this.u got some thing like ceil(),floor(),round() of math package which gives the largest number .ie Math.ceil(123.245)==124;Math.floor(123.245)==123;math.round(123.245)=123; But u dont have any function as such which can clear ur doubt.
Sadashiv Borkar
Ranch Hand

Joined: Jun 07, 2000
Posts: 49
import java.math.*;
public class BGDeciTest
public static void main(String[] args)
BigDecimal bg = new BigDecimal(123.245);
BigDecimal bg1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP);
I think within java docs there is a package called as java.math package.This we serve your purpose.I have given a sample programme above which will serve your purpose.
If doubts do reply.....
suggestions are welcomed......
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Java uses System.out.println(String) to send output to the stdout of your computer. On PC's this is the console unless you redirect it. To get formatted output, you have to format the String object the way you want it to look. For formating the appearance of numeric values, the most useful class is DecimalFormat. If I want my number 1234.567 to look like 1,234.57, I would use
DecimalFormat df = new DecimalFormat("#,###.00");
double d = 1234.567;
I gave the DecimalFormat a specific format that I want to use, You can create an instance that takes a locale or just uses the default locale to determine how to format things. Check out the documentation of DecimalFormat.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Tejal Modi

Joined: May 04, 2000
Posts: 3
Hi everybody
Thanks for your help I have used DecimalFormat to solve my problem.When I was seaching API for this i found 'setMaximumFractionDigits' method in the java.text package
public void setMaximumFractionDigits(int newValue)
Sets the maximum number of digits allowed in the fraction portion of a number. maximumFractionDigits must be >= minimumFractionDigits. If the new value for maximumFractionDigits is less than the current value of minimumFractionDigits, then minimumFractionDigits will also be set to the new value.
newValue - the maximum number of fraction digits to be shown; if less than zero, then zero is used. The concrete subclass may enforce an upper limit to this value appropriate to the numeric type being formatted.)
I have tried to make use of this but somehow getting compile errors.Would anybody please tell me how and when it can be used?
Thanx and Regards,
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
I agree. Here's the link:
subject: Format number
It's not a secret anymore!