I have a trouble, i'm doing calculators for school and i need to convert "E" to decimal in answer.
I have: 6.67E-11
I want: 6.67 * 10^-11
double m1 = Double.parseDouble(et1.getText().toString());
double m2 = Double.parseDouble(et2.getText().toString());
double R = Double.parseDouble(et3.getText().toString());
double answer = (6.67 * Math.pow(10, -11)) * ((m1 * m2) / sqrt(R));
tv_answer.setText(answer + "");
I can add this:
NumberFormat formatter = new DecimalFormat("###.####################");
String f = formatter.format(answer);
and edit:
tv_answer.setText(f + "");
But i will get something like this: 0.0000000000667. That's not cool :/
Not the best way, but you can try this.
BigDecimal d1 = new BigDecimal("0.0000000000000096");
DecimalFormat df = new DecimalFormat("0.0E0");
System.out.print(df.format(d1).replace("E"," * 10^"));
Output
9.6 * 10^-15
BigDecimal Example
double m1 = Double.parseDouble(et1.getText().toString());
double m2 = Double.parseDouble(et2.getText().toString());
double R = Double.parseDouble(et3.getText().toString());
double answer = BigDecimal.valueOf(6.67)
.multiply(BigDecimal.valueOf(Math.pow(10,-11)))
.multiply(BigDecimal.valueOf(m1).multiply(BigDecimal.valueOf(m2).divide(Math.sqrt(R),21,BigDecimal.ROUND_HALF_UP)))
.doubleValue();
tv_answer.setText(answer + "");
Related
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
decimalFormat.applyPattern(".00");
System.out.print(decimalFormat.format(63.275));
// output : 63.27
System.out.print(decimalFormat.format(64.275));
// output : 64.28
Why are they different?
The value of 63.275 is recorded in computer as 63.27499999999999857891452847979962825775146484375 . As per the Java API doc "https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html#HALF_UP " Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for RoundingMode.DOWN.
so ,
63.27499999999999857891452847979962825775146484375 ~ 63.27
64.275000000000005684341886080801486968994140625 ~ 64.28
public static double roundByPlace(double number, int scale){
BigDecimal bigDecimal = BigDecimal.valueOf(number);
String pattern = "0.";
for(int i = 0; i<scale; i++){
pattern = pattern+"0";
}
DecimalFormat decimalFormat = new DecimalFormat(pattern);
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
double result = Double.parseDouble(decimalFormat.format(bigDecimal));
return result;
}
I im currently working on a temprature converter app. Everything works but I can get over 5 decimals and I have tried to look it up and search on Google but can't find out how do it. Here is where I display the text in the main.java:
text = (EditText) findViewById(R.id.editText1);
result = (TextView) findViewById(R.id.tvResult);
float inputValue = Float.parseFloat(text.getText().toString());
DecimalFormat df = new DecimalFormat("#.00");
String s = (String.valueOf(ConvertFahrCels.convertCelsiusToFahrenheit(inputValue)));
String d = (String.valueOf(ConvertFahrCels.convertFahrenheitToCelsius(inputValue)));
if (celsiusButton.isChecked()) {
result.setText(d);
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
result.setText(s);
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
And here is where I calculate it:
// converts to celsius
public static float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// converts to fahrenheit
public static float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
Your code here implies it is creating a decimal format to do the work, but, you don't actually use it!
DecimalFormat df = new DecimalFormat("#.00");
String s = (String.valueOf(ConvertFahrCels.convertCelsiusToFahrenheit(inputValue)));
String d = (String.valueOf(ConvertFahrCels.convertFahrenheitToCelsius(inputValue)));
The code should be:
DecimalFormat df = new DecimalFormat("#.00");
String s = df.format(ConvertFahrCels.convertCelsiusToFahrenheit(inputValue));
String d = df.format(ConvertFahrCels.convertFahrenheitToCelsius(inputValue));
It is more common in Java now to use String formatting instead of decimal format. Consider:
String s = String.format("%.1f", ConvertFahrCels.convertCelsiusToFahrenheit(inputValue));
Finally, your question indicates you want 1 decimal place, but, the Decimal format you use adds two.
I want to summation in java.
So,
For example,
1.123 + 1.123E-4 = 0.0123411234
So, How can i processing "E" in java?
Use BigDecimal:
public static void main( String[] args ) {
BigDecimal bigDecimal1 = new BigDecimal( "1.123" );
BigDecimal bigDecimal2 = new BigDecimal( "1.123E-4" );
BigDecimal sum = bigDecimal1.add( bigDecimal2 );
System.out.println( sum );
}
Output:
1.1231123
Use the BigDecimal class. See http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String) for a constructor that will take a string like "1.123E-4".
You mean something like this?
String a = "1.123";
String b = "1.123E-4";
double d1 = Double.valueOf(a);
double d2 = Double.valueOf(b);
System.out.println("sum = " + (d1 + d2));
Double's valueOf() method can parse te E notation for you.
And actually, the result is: 1.1231123
Well, did you even try it?
final class SciNotationTest {
public static void main(final String[] argv) {
final double sum = 1.123 + 1.123E-4;
System.out.println(sum);
assert 1.1231123 == sum;
}
}
C:\dev\scrap>javac SciNotationTest.java
C:\dev\scrap>java -ea SciNotationTest
1.1231123
You should be careful when using double and testing equality without tolerance, as floating-point can often be imprecise. If high-precision is what you're striving for, indeed use BigDecimal.
Use Double.parseDouble() to do this...
String n1 = "1.123";
String n2 = "1.123E-4";
double dn1 = Double.parseDouble(n1);
double dn2 = Double.parseDouble(n2);
System.out.println("Total : " + (dn1 + dn2));
Output:
1.1231123
I have a series of Java decimals like:
0.43678436287643872
0.4323424556455654
0.6575643254344554
I wish to cut off everything after 5 decimal places. How is this possible?
If you want to keep things fast and simple. ;)
public static void main(String... args) {
double[] values = {0.43678436287643872, 0.4323424556455654, 0.6575643254344554,
-0.43678436287643872, -0.4323424556455654, -0.6575643254344554,
-0.6575699999999999 };
for (double v : values)
System.out.println(v + " => "+roundDown5(v));
}
public static double roundDown5(double d) {
return ((long)(d * 1e5)) / 1e5;
//Long typecast will remove the decimals
}
// Or this. Slightly slower, but faster than creating objects. ;)
public static double roundDown5(double d) {
return Math.floor(d * 1e5) / 1e5;
}
prints
0.43678436287643874 => 0.43678
0.4323424556455654 => 0.43234
0.6575643254344554 => 0.65756
-0.43678436287643874 => -0.43678
-0.4323424556455654 => -0.43234
-0.6575643254344554 => -0.65756
-0.6575699999999999 => -0.65756
float f = 0.43678436287643872;
BigDecimal fd = new BigDecimal(f);
BigDecimal cutted = fd.setScale(5, RoundingMode.DOWN);
f = cutted.floatValue();
The DecimalFormat could also be of assistance here:
double d = 0.436789436287643872;
DecimalFormat df = new DecimalFormat("0.#####");
df.setRoundingMode(RoundingMode.DOWN);
double outputNum = Double.valueOf(df.format(d));
String outpoutString = df.format(d);
Double.parseDouble(String.valueOf(x).substring(0,7));
OR
Double.valueOf(String.valueOf(x).substring(0,7));
where x contains the value you want to cut such as 0.43678436287643872
I believe the java.text.DecimalFormat class is what you need.
I would do it with regular expressions like this:
double[] values = {
0.43678436287643872,
0.4323424556455654,
0.6575643254344554,
-0.43678436287643872,
-0.4323424556455654,
-0.6575643254344554
};
Pattern p = Pattern.compile("^(-?[0-9]+[\\.\\,][0-9]{1,5})?[0-9]*$");
for(double number : values) {
Matcher m = p.matcher(String.valueOf(number));
boolean matchFound = m.find();
if (matchFound) {
System.out.println(Double.valueOf(m.group(1)));
}
}
The pattern can be easily modified if you need to support more/less decimal places.
To generalize Peter answer you can do:
public static double round(double n, int decimals) {
return Math.floor(n * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
I have created an application for android that takes three inputs and makes a calculation
else {
float result1 = (((new Float(input11.getText().toString())
+ new Float(input21.getText().toString()))/2));
float result2 = (new Float(input31.getText().toString());
if(result1<(result2 - 2)) {
result1 = result2-2;
float result=(float) ( (new Float(result1)*0.3)+(new Float(result2)*0.7));
vprosvasis.setText(Float.toString(result));
}
else if(result1>(result2 + 2)) {
result1=result2+2;
float result=(float) ( (new Float(result1)*0.3)+(new Float(result2)*0.7));
vprosvasis.setText(Float.toString(result));
}
else {
float result = (float) ((((new Float(input11.getText().toString())
+ new Float(input21.getText().toString()))/2)*0.3)
+ (new Float(input31.getText().toString())*0.7));
vprosvasis.setText(Float.toString(result));
}
}
Firstly,i would like the result in every statement to be e.x. 12.35 ,not 12,342323...
Secondly,i get in the same way vprosvasis2-vprosvasis7.i would like the final result that i print to be an integer and not a float..
float vprosvasisFloat = Float.parseFloat(vprosvasis.getText().toString());
float vprosvasisFloat2 = Float.parseFloat(vprosvasis2.getText().toString());
float vprosvasisFloat7 = Float.parseFloat(vprosvasis7.getText().toString());
float vprosvasisFloat5 = Float.parseFloat(vprosvasis6.getText().toString());
float vprosvasisFloat4 = Float.parseFloat(vprosvasis7.getText().toString());
float vprosvasisFloat3 = Float.parseFloat(vprosvasis6.getText().toString());
float vprosvasisFloat6 = Float.parseFloat(vprosvasis5.getText().toString());
float genikosvathmosoik = (( new Float(vprosvasis.getText().toString())
+ new Float(vprosvasis2.getText().toString())
+ new Float(vprosvasis3.getText().toString())
+ new Float(vprosvasis4.getText().toString())
+ new Float(vprosvasis5.getText().toString())
+ new Float(vprosvasis6.getText().toString())
+ new Float(vprosvasis7.getText().toString())) / 7);
moria2oik = (((new Float ((genikosvathmosoik*8)+(vprosvasisFloat * 1.3)+(vprosvasisFloat2 * 0.7))*100)));
moria3oik=(((new Float ((genikosvathmosoik*8)+(vprosvasisFloat4 * 1.3)+(vprosvasisFloat3 * 0.7))*100)));
moria5oik=(((new Float ((genikosvathmosoik*8)+(vprosvasisFloat7 *1.3)+(vprosvasisFloat6 * 0.7))*100)));
switch(spinner.getSelectedItemPosition()){
case 0:
show = new AlertDialog.Builder(mContext).setTitle(R.string.app_name)
.setMessage("1o : -\n2o : "
+ moria2oik + "\n3o : "
+ moria3oik + "\n4o : "
+ moria2oik + "\n5o : "
+ moria5oik)
.setPositiveButton("OK", null).show();
break;
If I understand the question correctly, you should look into the Math class of Java. Here is the developer doc.
The math class can do almost everything when it comes to manipulating a float or double.
The other class you might want to look at is DecimalFormat which can do exactly what is says, format decimals. :) That is available here.
You could take a look at java.text.NumberFormat. It will provide you methods to format your results in a user-friendly way, and also helps parsing according to the user's locale.