• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number Formatting

 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Sachin.
Maybe something like this?

This code returns

Hope this helps you, Sachin.
Art
 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic