There is a similar question on SO which suggests using NumberFormat which is what I have done.
I am using the parse() method of NumberFormat.
public static void main(String[] args) throws ParseException{
DecToTime dtt = new DecToTime();
dtt.decToTime("1.930000000000E+02");
}
public void decToTime(String angle) throws ParseException{
DecimalFormat dform = new DecimalFormat();
//ParsePosition pp = new ParsePosition(13);
Number angleAsNumber = dform.parse(angle);
System.out.println(angleAsNumber);
}
The result I get is
1.93
I didn't really expect this to work because 1.930000000000E+02 is a pretty unusual looking number, do I have to do some string parsing first to remove the zeros? Or is there a quick and elegant way?
Memorize the String.format syntax so you can convert your doubles and BigDecimals to strings of whatever precision without e notation:
This java code:
double dennis = 0.00000008880000d;
System.out.println(dennis);
System.out.println(String.format("%.7f", dennis));
System.out.println(String.format("%.9f", new BigDecimal(dennis)));
System.out.println(String.format("%.19f", new BigDecimal(dennis)));
Prints:
8.88E-8
0.0000001
0.000000089
0.0000000888000000000
When you use DecimalFormat with an expression in scientific notation, you need to specify a pattern. Try something like
DecimalFormat dform = new DecimalFormat("0.###E0");
See the javadocs for DecimalFormat -- there's a section marked "Scientific Notation".
If you take your angle as a double, rather than a String, you could use printf magic.
System.out.printf("%.2f", 1.930000000000E+02);
displays the float to 2 decimal places. 193.00 .
If you instead used "%.2e" as the format specifier, you would get "1.93e+02"
(not sure exactly what output you want, but it might be helpful.)
Related
I need to convert some floats to format like this: 2,5000E-003, 2,8625E+000
I know i can do it like this:
String.format("%s,%sE%s", digitInt, digitAfterDot, digitDegree)
But I hope somebody knows more clever and natty solution
You can use:
String.format("%1.4e",12345.67890123)
The result of this is:
1,2346e+04
Here is some documentation
EDIT
If you need 3 digits in the exponent, you can use the DecimalFormat Formatter:
DecimalFormat format = new DecimalFormat("0.0000E000");
System.out.print(format.format(12345.67890123f));
Which outputs:
1,2346E004
Note that it does not come with a positive sign if the exponent is positive.
You can fix this with:
DecimalFormat format = new DecimalFormat("0.0000E000");
String result = format.format(12345.67890123f);
if (!result.contains("E-")) {
result = result.replace("E", "E+");
}
i was made this method to show value of int into text view
texton= (TextView)findViewById(R.id.textcolumn);
texton.setText(String.valueOf(score));
so it will show like this
but i want to make the number text format like this, it is possible?
You can use DecimalFormat .
public class DecimalFormat extends NumberFormat
DecimalFormat is a concrete subclass of NumberFormat that formats
decimal numbers. It has a variety of features designed to make it
possible to parse and format numbers in any locale, including support
for Western, Arabic, and Indic digits. It also supports different
kinds of numbers, including integers (123), fixed-point numbers
(123.4), scientific notation (1.23E4), percentages (12%), and currency
amounts ($123). All of these can be localized.
Please have a look here
NumberFormat
How can I format a String number to have commas and round?
So Try this way ,
DecimalFormat formatter = new DecimalFormat("#,###.000");
String get_value = formatter.format(score);
texton= (TextView)findViewById(R.id.textcolumn);
texton.setText(get_value);
Logic Courtesy
You can use DecimalFormat before setting the value in textView
texton.setText(setDotsDigit(score));
public static String setDotsDigit(double value)
{
return new DecimalFormat("###.###.###").format(value);
}
You should use DecimalFormat to format your score value first.
DecimalFormat formatter = new DecimalFormat("#,###.00")`;
String s = formatter.format(score);
texton = (TextView)findViewById(R.id.textcolumn);
texton.setText(s);
Hope it will help you.
My question is about of toString() and toPlainString() methods of the BigDecimal dataTypewhich produces the output like
750.0000
150.0000
... etc
My question is how to specify the number of zeros followed after the dot? Is there a way to do it instead of String.replace(".0000", ".00") method?
Use DecimalFormat in combination with DecimalFormatSymbols:
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("##.##");
df.setDecimalFormatSymbols(dfs);
df.format(myNumber)
Without using DecimalFormatSymbols you would end up with a comma as a decimal seperator instead.
Please use the below code.
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
String s = nf.format(1111.2222);
System.out.println(s);
Apart from decimal format you can also use setScale(2) like this
new BigDecimal("1.0000").setScale(2)
Also setScale allows you can specify the Rounding Mode
You could use setScale method and optinally you could choose rounding methodology of your own. Somethign like:
BigDecimal b = new BigDecimal("750.0000");
b.setScale(2);
DecimalFormat df2 = new DecimalFormat("#.##");
double zipf = 0.23951367781155017;
String zipt = df2.format(zipf);
System.out.println(zipt);
And I get "0,24"
The problem with this is then I want to use it as a double. But the Double.valueOf(); method fails due to the comma being there in the string output. Any way to solve this?
For decimal dot, you should create an instance with english locale like this:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String zipt = nf.format(zipf);
System.out.println(zipt);
I also suggest setting rounding to HALF_UP, because default rounding is not what most of us would expect: http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN
nf.setRoundingMode(RoundingMode.HALF_UP);
Use different locale.German has dot
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;
Alternative woud be to use string and then modify string to your needs.After that just parse to double.All done :)
Your problems is the local that your JVM is using , try to change at your current local.
Use DecimalFormat constructor that allows you to specify locale
new DecimalFormat("#.##", new DecimalFormatSymbols(new Locale("en")));
you could "format" your double manually but cutting of the decimal places like this:
DecimalFormat df2 = new DecimalFormat("#.##");
double zipf = 0.23951367781155017;
String zipt = df2.format(zipf);
System.out.println(zipt);
long zipfLong = Math.round(zipf*100);
double zipfDouble = zipfLong/100.0;
System.out.println(zipfDouble);
with Math.round you make sure the that 0.239.. becomes 0.24. zipf*100 will "cut" off the additional decimal places and zipfLong/100.0 will add the decimal places again. Sorry, bad explanation but here is the output:
0,24
0.24
And you can reuse the new zipfDouble as a double value without casting or taking care of locale settings.
Good day.
I need to format a number in java.
So far I have this:
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
System.out.println(new Double(df2.format(balance)).doubleValue());
But it prints out this
110.0
121.0
133.1
146.41
161.05
But I need it to be with two digits in fraction part. How do I do it?
You don't have to get double value from formatted string.
Just use formatted string, which is returned from format() method of DecimalFormat.
So your code should be like the following:
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
...
System.out.println(df2.format(balance));
Your original code:
System.out.println(new Double(df2.format(balance)).doubleValue());
What you did in your code is: format the double value to string(which is formatted as you specified in the DecimalFormat instance). Then you convert the formatted string to Double instance and get double value from the object, which is double. And then printed it to console. So the formatted string is gone, and the double value is printed as normal.
"But I need it to be with two digits in fraction part. How do I do it?"
DecimalFormat df2 = new DecimalFormat( );
df2.setMinimumFractionDigits(2);
df2.setMaximumFractionDigits(2);
System.out.println(df2.format(balance));
You could also use the setMinimumFractionDigits method of DecimalFormat
df2.setMinimumFractionDigits(2);
your decimal format is right, but
what you are doing before you print this out is new Double(df2.format(balance)) which create new instant of double, which ignores your formatting.
so if you want to display or log your value df2.format(balance) this should be enough
ie:
System.out.println(df2.format(balance));
Try this pattern for formatting #,###,###,##.##-
DecimalFormat df2 = new DecimalFormat( "#,###,###,##.##" );
System.out.println(df2.format(balance));
This should be sufficient:
DecimalFormat df2 = new DecimalFormat("#,##0.00");
System.out.println(df2.format(balance));
The grouping for separator will follow "the interval between the last one and the end of the integer". So there is no benefit from over-specify. Example from the documentation of DecimalFormat:
The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
Another thing is that .format() method already output a String, so there is no point in converting it to double. It will cause Exception to be thrown when balance is more than 1000 (the point when separator comes into effect, and Double class cannot parse the String with separator).