Decimal display for very large/small numbers

G

GGP

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

Andrew Thompson via JavaKB.com

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>
 
G

GGP

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;
}
}
}
 
A

Andrew Thompson via JavaKB.com

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top