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+");
}
Related
i want to format my double value to 2 decimals and then make it "text to speech".
this is my code:
mares = mass * acc;
DecimalFormat df = new DecimalFormat("#.00");
df.format(mares);
String mare = String.format("The force is %f", df);
home.speak(mare,TextToSpeech.QUEUE_FLUSH, null);
but it crashes, i don't know why, i put 5 and 6 and it should multiply them and give me 30.00 or something like that.
when i remove DecimalFormat the result is 30.00000000000000, i just don't like it, too many zeros.
can someone help me please?
Thanks in advance!
Your DecimalFormat is returning the formatted string, but you are ignoring it, and passing it as an argument to String.format, which certainly isn't right.
Assign the return of df.format to a string for further reference:
String mare = df.format(mares);
Or pass the numeric value directly to String.format, with the appropriate format precision specified:
String mare = String.format("The force is %.2f", mares);
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);
Suppose we have a BigDecimal variable setted up like this, and we can't change the way of it's creation:
BigDecimal num = new BigDecimal(6.0053);
So we'll see the following 'pretty' value after an output:
System.out.println(num); //0053000000000000824229573481716215610504150390625
The only way I've found to resolve this problem was using BigDecimal.valueOf():
System.out.println(BigDecimal.valueOf(num.doubleValue())); //6.0053
But is there a more simple solution?
Thanks in advance.
Use java.text.DecimalFormat to format the numbers.
NumberFormat format = new DecimalFormat("0.####");
System.out.println(format.format(num));
The format options can be found here
BigDecimal num = new BigDecimal(6.0053);
DecimalFormat df=new DecimalFormat("0.0000");
System.out.println(df.format(num));
You can use String.format with the "g" formatting type. It allows you to control how many digits you want, etc. See here for all formatting options.
BigDecimal num = new BigDecimal(6.0053);
String formated = String.format("%6.5g", num);
System.out.println(formated);
(Result : 6.0053)
System.out.println(new DecimalFormat("###,##0.00").format(myBigDecimalVariable));
I have a string similar to: 7.6E+7.
My question is simple: How do I turn this into its corresponding number: 76000000?
I have tried using substring to isolate the E+7 part, then parse the 7 part, then move the decimal places over 7. Is there an easier way to do this?
Thank you!
long n = Double.valueOf("7.6E+7").longValue();
System.out.println(d);
// prints 76000000 to the output.
I suggest using Double.parseDouble():
double val = Double.parseDouble(str);
where str is the input string.
You can use Double.parseDouble() to get it as a number.
String e = "7.6E+7";
System.out.println(Double.parseDouble(e));
Giving the output 7.6E7. If you do not want the E in the output you can use
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
System.out.println(f.format(Double.parseDouble(e)));
Which will give you the output 76000000 without casting to a whole number. Eg adding 0.1 to the number will give the output 76000000.1
If you are sure the number can ultimately be cast into an integer without losing precision than alternatively you could do:
int d = (int) Double.parseDouble("7.6E+7");
System.out.println(d);
Which prints 76000000 to the output.
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.)