We're updating the issue view to help you get more done. 

Better DecimalFormat constructor format string handling

Description

Sample code:
DecimalFormat decF4 = new DecimalFormat(",##0.0000");
DecimalFormat noDecFmt = new DecimalFormat(",##0");

  • decF3 is meant to show 4 decimal places and handle commas before the decimal
    place.

  • noDecFmt is meant to show no decimal places and handle commas.

  • Rounding in both places is meant to be half up.

Problem 1: simply running
decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work as
decF4.setRoundingIncrement(.0001) must also be run.
Problem 2: decF4.format(8.88885) does not return 8.8889 as expected. You must
run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in order for this to
work as expected.
Problem 3: There seems to be no way to set half up to be the default rounding
mode.

We solved the problem with the code at the bottom of this page however this is
not quite general purpose enough to include in icu4j. A static
setDefaultRoundingMode function would solve the problem nicely. Also decimal
places past 20 are not handled properly. A small ammount of work would make
bring this up to snuff.

— source of com.itginc.text.DecimalFormat

package com.itginc.text;

import java.text.FieldPosition;

/**

  • Title: Webtrade

  • Description:

  • Copyright: Copyright (c) 2000

  • Company: ITG Inc.

  • @author

  • @version 1.0
    */

public class DecimalFormat extends com.ibm.text.DecimalFormat {
public DecimalFormat() {
super();
}
public DecimalFormat(String p0) {
super(p0);
setRoundingInfo(p0);

}
public DecimalFormat(String p0, com.ibm.text.DecimalFormatSymbols p1) {
super(p0,p1);
setRoundingInfo(p0);
}

public StringBuffer format(double parm1, StringBuffer parm2, FieldPosition
parm3) {
return super.format( new com.ibm.math.BigDecimal(Double.toString(parm1)),
parm2, parm3);
}

// used to fix the rounding
protected void setRoundingInfo(String p0) {
setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP);
setScientificNotation(false);
int decimalLocation =
p0.indexOf(getDecimalFormatSymbols().getDecimalSeparator());
if (decimalLocation > 0) {
int negPow = p0.length() - decimalLocation - 1;
double increment = 1;
switch (negPow) {
case 1: increment = .1; break;
case 2: increment = .01; break;
case 3: increment = .001; break;
case 4: increment = .0001; break;
case 5: increment = .00001; break;
case 6: increment = .000001; break;
case 7: increment = .0000001; break;
case 8: increment = .00000001; break;
case 9: increment = .000000001; break;
case 10: increment = .0000000001; break;
case 11: increment = .00000000001; break;
case 12: increment = .000000000001; break;
case 13: increment = .0000000000001; break;
case 14: increment = .00000000000001; break;
case 15: increment = .000000000000001; break;
case 16: increment = .0000000000000001; break;
case 17: increment = .00000000000000001; break;
case 18: increment = .000000000000000001; break;
case 19: increment = .0000000000000000001; break;
case 20: increment = .00000000000000000001; break;
}
if (increment != 1) {
setRoundingIncrement(increment);
} else {
// do something better here!!
}
} else {
setRoundingIncrement(1);
setDecimalSeparatorAlwaysShown(false);
setMaximumFractionDigits(0);
}
}
}

Environment

Status

Assignee

TracBot

Reporter

TracBot

Labels

tracCreated

Apr 22, 2002, 11:50 PM

tracOwner

syn wee

tracProject

ICU4J

tracReporter

barker@ab8837c3c73617f5

tracResolution

fixed

tracReviewer

alan

tracStatus

closed

Components

Fix versions

Priority

major