Decimal display for very large/small numbers

Discussion in 'Java' started by GGP, Apr 2, 2007.

  1. GGP

    GGP Guest

    I have an app that receives input from a user, executes a calculation,
    and displays a numerical result. The user may choose (or so the
    theory goes) the number of decimal places s/he prefers in the output
    (ranges between 0 and 10). However, I'm having a tough time figuring
    out how to do this.

    The input is a text box, and the text value is converted to a
    BigDecimal. If the result is, say, 123.4567 and the user wishes to
    limit the decimal places to three, my code works well, and the output
    would be 123.457. That works. However, if the result is a very large/
    small number I can't seem to get the proper output (for very large
    numbers, the output I get is 0,000,000 and for very small numbers it's
    just 0).

    My goal is to set a limit for very large/small numbers where, once
    reached, the result format will switch to scientific notation while
    still respecting the user's choice for number of decimal places.

    Here's some of my code:

    int precision = 3; //this is read from a UI control and
    successfully converted to an int (= no. decimals)
    DecimalFormat decimals = new DecimalFormat();
    Double dbl = null;
    Double ddbl = dbl.parseDouble(txtbxConvertFrom.getText());
    BigDecimal convertValue = BigDecimal.valueOf(ddbl);
    Converter converter = new Converter(parameters);
    BigDecimal convertedResult = converter.getConvertedUnit(); //This
    works fine
    //This last line results in the semi-erroneous behaviour cited above.

    I've also tried working with 'new MathContext(precision)', which
    successfully switches to scientific notation when the number is too
    big or too small, but the output uses the variable 'precision' to set
    the significant figures, not the decimal places (e.g., a result might
    be 1.23E6, for example, which appropriately has 3 significant figures,
    but only two decimal places where three is needed).

    My question is, how can I obtain a result that consistently respects
    the user-selected number of decimal places, even after switching to
    scientific notation?

    BTW, there seems to be far too many possibilities to simply apply a
    particular format (unless there's some way to do this from code--which
    I've tried rather unsuccessfully), such as #.## (or whatever).

    Thanks for considering this. Sincerely,

    GGP, Apr 2, 2007
    1. Advertising

  2. GGP wrote:
    >Thanks for considering this. ..

    I considered your code to the point of adding the wrappers
    for a class and main, fixing the wrapped lines, and adding a
    few imports. At that stage, I was still getting 5 compile errors
    based around the absence of txtbxConvertFrom and Converter.

    That's when I gave up.

    Post an SSCCE, and I might provide some further

    Andrew Thompson

    Message posted via
    Andrew Thompson via, Apr 2, 2007
    1. Advertising

  3. GGP

    GGP Guest

    On Apr 2, 2:51 am, "Andrew Thompson via" <u32984@uwe>

    > Post an SSCCE, and I might provide some further
    > consideration.

    Thanks for thinking about it, and for the URL. Although I should have
    been, I wasn't familiar with SSCCE. Your suggestion that preparing an
    SSCCE almost always helps in solving the problem was correct in this
    case. I managed to answer my own question by going through the
    exercise (after a weekend of frustration!). So, for the sake of
    completeness, here's the correct code.

    import java.math.BigDecimal;
    import java.math.MathContext;
    import java.text.DecimalFormat;

    public class TestDecimals {

    public TestDecimals() {

    public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(;
    BufferedReader buf_in = new BufferedReader(reader);
    String read = "0"; //exit string (zero)

    do {
    int precision = 6; //User input
    DecimalFormat decimals = new DecimalFormat();
    Double dbl = null;
    read = buf_in.readLine();
    Double ddbl = dbl.parseDouble(read); //User input
    BigDecimal convertValue = BigDecimal.valueOf(ddbl);
    BigDecimal convertedResult = converter(ddbl);
    if (convertedResult.doubleValue()>10E6 ||
    convertedResult.doubleValue()<10E-6) {
    BigDecimal bd = convertedResult.round(new
    MathContext(precision + 1));
    System.out.println("The result (MathContext adjusted)
    is: " + bd);
    } else {
    System.out.println("The result (decimal format
    adjusted) is: " + decimals.format(convertedResult).toString());
    } while (!read.toLowerCase().equals("0"));

    public static BigDecimal converter(Double testValue) {
    if (testValue > 1000) {
    BigDecimal result = new BigDecimal(testValue / 10E-6);
    return result;
    } else if (testValue < 0.0001) {
    BigDecimal result = new BigDecimal(testValue / 10E6);
    return result;
    } else {
    BigDecimal result = new BigDecimal(testValue);
    return result;
    GGP, Apr 2, 2007
  4. GGP wrote:
    >... I managed to answer my own question by going through the
    >exercise (after a weekend of frustration!).


    >...So, for the sake of
    >completeness, here's the correct code.

    OK.. let's give it a test run..
    (whispers after fising a couple of long, wrapped lines)


    The result (decimal format adjusted) is: 21.000350
    The result (decimal format adjusted) is: 420.000000
    The result (MathContext adjusted) is: 4.000000E+11
    The result (decimal format adjusted) is: 7.255556
    Exception in thread "main" java.lang.NumberFormatException: For input string:
    at sun.misc.FloatingDecimal.readJavaFormatString(

    LOL! OK - I suppose that last one was expecting
    a bit much. I just couldn't resist. ;-)

    Glad you sorted it.

    Andrew Thompson

    Message posted via
    Andrew Thompson via, Apr 2, 2007
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M.-A. Lemburg
    M.-A. Lemburg
    Apr 4, 2005
  2. Replies:
  3. pereges

    #define for very small numbers ?

    pereges, Apr 13, 2008, in forum: C Programming
    Harald van Dijk
    Apr 15, 2008
  4. ericnoonan
    Dec 14, 2009
  5. bmm

Share This Page