Link to home
Create AccountLog in
Avatar of cofactor
cofactor

asked on

Issue with DecimalFormat

I'm using DecimalFormat

DecimalFormat f = new DecimalFormat("############.00")

when I have 0  , I get output   .00  .

I want if value is 0 , after  format I want to get  0.00 .

How ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of cofactor
cofactor

ASKER

@CHEJ,

I may have a number like this ..

123456.23  // there is no 0 digit here.

expected out is 123456.23

Will  it  still work ?
It will if the decimal part is not nothing.
I've been looking at this and can't make a lot of sense from it. I will post the code here that I am using to see what's going on. I'll leave the commented out junk in there so it can be seen what I'm up to.

import java.text.*;
import java.math.*;


class DecFormat {

static DecimalFormat df;
static BigDecimal bd;
static Number num;
static Double d;
static Float dd;


	public static void main(String[] args){

		DecimalFormat f = new DecimalFormat("###########0.00");//CEHJ's.
		
		df = new DecimalFormat();
		//df = new DecimalFormat();
		//df.setMinimumFractionDigits(2);
				
		//df.applyPattern("##,###,###.00;(##,###,###.00)");
		

				//df.setRoundingMode(RoundingMode.valueOf("UNNECESSARY") );

		//num = df.parse("12345678.00",new ParsePosition(0));
		//num = f.parse("12345678.00",new ParsePosition(0));
num = f.parse("123456.23",new ParsePosition(0));
				
		System.out.println(num);
		

	}


}

Open in new window

double dub = 12345678.00;
DecimalFormat def = new DecimalFormat();

def.applyPattern("##,###,###.00;(##,###,###.00)");

System.out.print(def.format(dub));

Open in new window



OR .... you can do this:

double dub = 12345678.00;
DecimalFormat def = new DecimalFormat();

def.applyPattern("##,###,###.##;(##,###,###.##)");

def.setMinimumFractionDigits(2);

System.out.print(def.format(dub));

Open in new window