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
    decimals.setMaximumFractionDigits(precision);
    decimals.setMaximumIntegerDigits(7);
    txtbxConvertTo.setText(decimals.format(convertedResult).toString());
    //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,

    Greg.
    GGP, Apr 2, 2007
    #1
    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
    consideration.
    <http://www.physci.org/codes/sscce.html>

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via http://www.javakb.com
    Andrew Thompson via JavaKB.com, Apr 2, 2007
    #2
    1. Advertising

  3. GGP

    GGP Guest

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

    > 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.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    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(System.in);
    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);
    decimals.setMaximumFractionDigits(precision);
    decimals.setMinimumFractionDigits(precision);
    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
    #3
  4. GGP wrote:
    ..
    >... I managed to answer my own question by going through the
    >exercise (after a weekend of frustration!).


    Champ!

    >...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)

    >import java.io.BufferedReader;


    21.00035
    The result (decimal format adjusted) is: 21.000350
    420
    The result (decimal format adjusted) is: 420.000000
    4e6
    The result (MathContext adjusted) is: 4.000000E+11
    7.2555555555555555
    The result (decimal format adjusted) is: 7.255556
    6*10^7
    Exception in thread "main" java.lang.NumberFormatException: For input string:
    "6*10^7"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)

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

    Glad you sorted it.

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
    Andrew Thompson via JavaKB.com, Apr 2, 2007
    #4
    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
    Replies:
    3
    Views:
    552
    M.-A. Lemburg
    Apr 4, 2005
  2. Replies:
    0
    Views:
    286
  3. pereges

    #define for very small numbers ?

    pereges, Apr 13, 2008, in forum: C Programming
    Replies:
    22
    Views:
    662
    Harald van Dijk
    Apr 15, 2008
  4. ericnoonan
    Replies:
    0
    Views:
    336
    ericnoonan
    Dec 14, 2009
  5. bmm
    Replies:
    0
    Views:
    130
Loading...

Share This Page