File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Format number" Watch "Format number" New topic
Author

Format number

Tejal Modi
Greenhorn

Joined: May 04, 2000
Posts: 3
hi,
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?
Thanks
tejal
naren
Greenhorn

Joined: Aug 06, 2000
Posts: 9
hi,
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);
System.out.println(bg1.floatValue());
}
}
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......
bye.....
REGARDS
Sadashiv
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Hello,
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;
System.out.println(df.format(d));
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
Greenhorn

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
as:
(setMaximumFractionDigits
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.
Parameters:
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,
Tejal
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Format number
 
Similar Threads
Crypted Strings in a file
What java methods to use?
hi, i have problem in reading data
i love java
Why we choose java?