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 Number Formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Number Formatting" Watch "Number Formatting" New topic
Author

Number Formatting

Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
Hi,
I have variables (double) which are very long (values like 0.03569409851532) How do I round them to 4(or n) places after the decimal point so that the above value changes to 0.0357?
Thanx in advance
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can take a look at java.math.BigDecimal class there is also an api to format number in java.text.NumberFormat but this is quite hard to use. I try too used it once but finally i implement a little class that do the work....but it was a long time ago.....
import java.math.*;
import java.text.DecimalFormat;
import java.text.*;
import java.util.*;
public class NumberProvider
{

private static char decimalSeparator = ',';
private static char groupSeparator = '.';
private static int groupSize = 3;
public static synchronized String getFormatedString (BigDecimal number){

String brutoNumber = number.abs().toString();
if (brutoNumber.indexOf('.')!=-1)
{
brutoNumber = brutoNumber.substring(0,brutoNumber.indexOf('.'))+brutoNumber.substring(brutoNumber.indexOf('.')+1);
}
StringBuffer numberBuffer = new StringBuffer();
int scale= number.scale();
//System.out.println("scale = "+brutoNumber);
int intGroup = -1;
boolean beginGroup;
if (scale==0){
beginGroup=true;
intGroup=0;
}
else {
beginGroup=false;
}
for (int i = brutoNumber.length()-1; i>=0 ; i--){
numberBuffer.insert(0,brutoNumber.charAt(i));
if (i == brutoNumber.length()-scale ){
beginGroup = true;
numberBuffer.insert(0,decimalSeparator);
}
if (beginGroup){
intGroup++;
if (intGroup == groupSize && i!=0){
intGroup=0;
numberBuffer.insert(0,groupSeparator);
}
}

}
if (number.signum()==-1){
numberBuffer.insert(0,'-');
}

return numberBuffer.toString();
}
}
------------------
Benjamin l´┐Żonard
evisor
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Sachin.
Maybe something like this?

This code returns

Hope this helps you, Sachin.
Art
Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
Thanx Art,
I tried it your way and it seems to working fine.
Sorry Benjamin, but I got bogged down by the size of your code.
But I dont understand this: is there no direct way of formatting decimals in java like there is in good ol' 'c'?
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Originally posted by Art Metzer:
[B][/B]

Art, I've been trying to follow your code and I have a few questions. First, why do you call round() only to set the int parameter (which is hardcoded) and then call round(int)? Why not just call round(int) directly from the main method?
[B]
This code returns

[/B]

What I can't figure out is HOW this works? template.append( "0" ); appears to only replace all the characters beyond the 4th place with "0"...where is the rounding funciton for the 4th place?
Sorry if any of this is obvious, I'm just trying to feel my way along here...
Thanks!
------------------
I'm a soldier in the NetScape Wars...
Joel


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Joel, great questions.

[W]hy do you call round() only to set the int parameter (which is hardcoded) and then call round(int)?

I made an assumption in my design that anybody who would invoke this class's methods would be invoking it repeatedly, perhaps to display values on a report or in a GUI. If that's true, then I also assumed you would want a consistent rounding: whether it's four decimal places or two or whatever, you'd probably always want the same number of decimal places in the result. And if that's true, I assumed any programmer wouldn't want to have to invoke "round( 4 )" repeatedly, since her desired number of decimal places is always going to be the same. That said, I also didn't want to shut out anyone else who would want to use the class, but for a different number of decimal places. So I also made the overridden version "round( int places )" available. In short, the "round( int places )" version came first, because it contains the implementation, and I wrote a "round()" version as a default for the lazy. It's a judgment call on my part, and it definitely doesn't have to be that way.

What I can't figure out is HOW this works? template.append( "0" ); appears to only replace all the characters beyond the 4th place with "0"...where is the rounding funciton for the 4th place?

This rounding takes place automatically as a part of the java.text.DecimalFormat class.
Hope this helps, Joel. Please let me know if you have any more questions.
Art
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Maybe I'm oversimplifying, but if you want 4 decimal places

ie shift the decimal down 4 places, cast to remove the rest and shift the decimal back up...
Obviously it can be done for arbitrary precision.
Dave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number Formatting