Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Format number

 
Tejal Modi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tejal Modi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic