How can i get sum in java? - java

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

Related

How to express "1.275" to "1.28" with Decimal Format in Java

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

Bigdecimal restriction whole and decimal part

How do you restrict the whole part to 15 and decimal part to 8 like 15.8 using BigDecimal
For Example:
String quant= "1000";
String price = "123456789012345.12345678";
final int contant = 100;
BigDecimal bd1;
BigDecimal bd2;
String value = "";
bd1 = new BigDecimal(price).multiply(new BigDecimal(quant));
bd2 = bd1.divide(new BigDecimal(contant));
value = bd2.toPlainString();
Output value coming as 1234567890123451.2345678. So can I restrict the whole part up to 15 and decimal part up to 8.
If you restrict the 'whole' part to 15, then you will lose precision, why would you do that?
You can restrict (round) the decimal part by using BigDecimal.setScale. setScale can take an integer (how many decimal parts should be used) and a rounding strategy, e.g. BigDecimal.ROUND_UP.
Check the JavaDoc from BigDecimal for further information.
You can validate as follows
String price = "123456789012345.12345678";
String[] str=price.split("\\.");
if(!((str[0].length()==15)&&(str[1].length()==8))){
System.out.println("not a valid format");
}
Not sure why you would do that, but here is a way-
int contant = 100;
String price = "123456789012345.12345678224423";
String quant = "1000";
BigDecimal bd1 = new BigDecimal(price).multiply(new BigDecimal(quant));
BigDecimal bd2 = bd1.divide(new BigDecimal(contant));
final int WHOLE_MAX = 15;
final int SCALE = 8;
String value = bd2.setScale(SCALE, RoundingMode.HALF_UP).toPlainString();
String tempWhole = value.substring(0, value.indexOf('.')); // Digits before the decimal point
String wholePart = tempWhole.substring(0, Math.min(tempWhole.length(), WHOLE_MAX)); // Gets the first 15 digits
String decimalPart = value.substring(value.indexOf('.'));
value = wholePart.concat(decimalPart);
System.out.println(value);
Output:
123456789012345.23456782
The BigDecimal is described with precision. You can set it using MathContext.
MathContext context = new MathContext(15 + 8, RoundingMode.HALF_UP);
When you apply to your need
BigDecimal value = new BigDecimal("1234567890123456789.12345678",context);
The out put will be
1234567890123456789.1235
This is the expected way that what is erased from the number is the smallest thing. If you wan to set upper limit for the number you will need to declare your own type and check that result is grater then limit, then you have an overflow error.
You can implement your type that controls it by extending BigDecimal class
private class BigNumber extends BigDecimal {
private final int exponent;
private final int mantissa;
private BigDecimal limit;
public BigNumber(String val, int exponent, int mantissa) {
super(val);
this.exponent = exponent;
this.mantissa = mantissa;
char[] limit = new char[exponent];
Arrays.fill(limit, '9');
this.limit = new BigDecimal(limit).add(BigDecimal.ONE);
}
#Override
public BigDecimal add(BigDecimal augend) {
BigDecimal result = super.add(augend);
if(result.compareTo(limit) < 0) {
return result;
}
throw new RuntimeException("Nuber to large");
}
}

How to cut off decimal in Java WITHOUT rounding?

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

Convert float to String and String to float in Java

How could I convert from float to string or string to float?
In my case I need to make the assertion between 2 values string (value that I have got from table) and float value that I have calculated.
String valueFromTable = "25";
Float valueCalculated =25.0;
I tried from float to string:
String sSelectivityRate = String.valueOf(valueCalculated);
but the assertion fails
Using Java’s Float class.
float f = Float.parseFloat("25");
String s = Float.toString(25.0f);
To compare it's always better to convert the string to float and compare as two floats. This is because for one float number there are multiple string representations, which are different when compared as strings (e.g. "25" != "25.0" != "25.00" etc.)
Float to string - String.valueOf()
float amount=100.00f;
String strAmount=String.valueOf(amount);
// or Float.toString(float)
String to Float - Float.parseFloat()
String strAmount="100.20";
float amount=Float.parseFloat(strAmount)
// or Float.valueOf(string)
You can try this sample of code:
public class StringToFloat
{
public static void main (String[] args)
{
// String s = "fred"; // do this if you want an exception
String s = "100.00";
try
{
float f = Float.valueOf(s.trim()).floatValue();
System.out.println("float f = " + f);
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException: " + nfe.getMessage());
}
}
}
found here
I believe the following code will help:
float f1 = 1.23f;
String f1Str = Float.toString(f1);
float f2 = Float.parseFloat(f1Str);
This is a possible answer, this will also give the precise data, just need to change the decimal point in the required form.
public class TestStandAlone {
/**
* This method is to main
* #param args void
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Float f1=152.32f;
BigDecimal roundfinalPrice = new BigDecimal(f1.floatValue()).setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("f1 --> "+f1);
String s1=roundfinalPrice.toPlainString();
System.out.println("s1 "+s1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output will be
f1 --> 152.32
s1 152.32
If you're looking for, say two decimal places..
Float f = (float)12.34;
String s = new DecimalFormat ("#.00").format (f);
well this method is not a good one, but easy and not suggested. Maybe i should say this is the least effective method and the worse coding practice but, fun to use,
float val=10.0;
String str=val+"";
the empty quotes, add a null string to the variable str, upcasting 'val' to the string type.
There are three ways to convert float to String.
"" + f
Float.toString(f)
String.valueOf(f)
There are two ways Convert String to float
Float.valueOf(str)
Float.parseFloat(str);
Example:-
public class Test {
public static void main(String[] args) {
System.out.println("convert FloatToString " + convertFloatToString(34.0f));
System.out.println("convert FloatToStr Using Float Method " + convertFloatToStrUsingFloatMethod(23.0f));
System.out.println("convert FloatToStr Using String Method " + convertFloatToStrUsingFloatMethod(233.0f));
float f = Float.valueOf("23.00");
}
public static String convertFloatToString(float f) {
return "" + f;
}
public static String convertFloatToStrUsingFloatMethod(float f) {
return Float.toString(f);
}
public static String convertFloatToStrUsingStringMethod(float f) {
return String.valueOf(f);
}
}
String str = "1234.56";
float num = 0.0f;
int digits = str.length()- str.indexOf('.') - 1;
float factor = 1f;
for(int i=0;i<digits;i++) factor /= 10;
for(int i=str.length()-1;i>=0;i--){
if(str.charAt(i) == '.'){
factor = 1;
System.out.println("Reset, value="+num);
continue;
}
num += (str.charAt(i) - '0') * factor;
factor *= 10;
}
System.out.println(num);
To go the full manual route: This method converts doubles to strings by shifting the number's decimal point around and using floor (to long) and modulus to extract the digits. Also, it uses counting by base division to figure out the place where the decimal point belongs. It can also "delete" higher parts of the number once it reaches the places after the decimal point, to avoid losing precision with ultra-large doubles. See commented code at the end. In my testing, it is never less precise than the Java float representations themselves, when they actually show these imprecise lower decimal places.
/**
* Convert the given double to a full string representation, i.e. no scientific notation
* and always twelve digits after the decimal point.
* #param d The double to be converted
* #return A full string representation
*/
public static String fullDoubleToString(final double d) {
// treat 0 separately, it will cause problems on the below algorithm
if (d == 0) {
return "0.000000000000";
}
// find the number of digits above the decimal point
double testD = Math.abs(d);
int digitsBeforePoint = 0;
while (testD >= 1) {
// doesn't matter that this loses precision on the lower end
testD /= 10d;
++digitsBeforePoint;
}
// create the decimal digits
StringBuilder repr = new StringBuilder();
// 10^ exponent to determine divisor and current decimal place
int digitIndex = digitsBeforePoint;
double dabs = Math.abs(d);
while (digitIndex > 0) {
// Recieves digit at current power of ten (= place in decimal number)
long digit = (long)Math.floor(dabs / Math.pow(10, digitIndex-1)) % 10;
repr.append(digit);
--digitIndex;
}
// insert decimal point
if (digitIndex == 0) {
repr.append(".");
}
// remove any parts above the decimal point, they create accuracy problems
long digit = 0;
dabs -= (long)Math.floor(dabs);
// Because of inaccuracy, move to entirely new system of computing digits after decimal place.
while (digitIndex > -12) {
// Shift decimal point one step to the right
dabs *= 10d;
final var oldDigit = digit;
digit = (long)Math.floor(dabs) % 10;
repr.append(digit);
// This may avoid float inaccuracy at the very last decimal places.
// However, in practice, inaccuracy is still as high as even Java itself reports.
// dabs -= oldDigit * 10l;
--digitIndex;
}
return repr.insert(0, d < 0 ? "-" : "").toString();
}
Note that while StringBuilder is used for speed, this method can easily be rewritten to use arrays and therefore also work in other languages.

Format double to at least one significant digit in Java/Android

I have a DecimalFormat object which I'm using to format all of my double values to a set number of digits (let's say 2) when I'm displaying them. I would like it to normally format to 2 decimal places, but I always want at least one significant digit. For example, if my value is 0.2 then my formatter spits out 0.20 and that's great. However, if my value is 0.000034 my formatter will spit out 0.00 and I would prefer my formatter spit out 0.00003.
The number formatters in Objective-C do this very simply, I can just set a max number of digits I want to show at 2 and the minimum number of significant digits at 1 and it produces my desired output, but how can I do it in Java?
I appreciate any help anyone can offer me.
Kyle
Edit: I'm interested in rounding the values so 0.000037 displays as 0.00004.
It's not efficient, so if you perform this operation often I'd try another solution, but if you only call it occasionally this method will work.
import java.text.DecimalFormat;
public class Rounder {
public static void main(String[] args) {
double value = 0.0000037d;
// size to the maximum number of digits you'd like to show
// used to avoid representing the number using scientific notation
// when converting to string
DecimalFormat maxDigitsFormatter = new DecimalFormat("#.###################");
StringBuilder pattern = new StringBuilder().append("0.00");
if(value < 0.01d){
String s = maxDigitsFormatter.format(value);
int i = s.indexOf(".") + 3;
while(i < s.length()-1){
pattern.append("0");
i++;
}
}
DecimalFormat df = new DecimalFormat(pattern.toString());
System.out.println("value = " + value);
System.out.println("formatted value = " + maxDigitsFormatter.format(value));
System.out.println("pattern = " + pattern);
System.out.println("rounded = " + df.format(value));
}
}
import java.math.BigDecimal;
import java.math.MathContext;
public class Test {
public static void main(String[] args) {
String input = 0.000034+"";
//String input = 0.20+"";
int max = 2;
int min =1;
System.out.println(getRes(input,max,min));
}
private static String getRes(String input,int max,int min) {
double x = Double.parseDouble(((new BigDecimal(input)).unscaledValue().intValue()+"").substring(0,min));
int n = (new BigDecimal(input)).scale();
String res = new BigDecimal(x/Math.pow(10,n)).round(MathContext.DECIMAL64).setScale(n).toString();
if(n<max){
for(int i=0;i<max;i++){
res+="0";
}
}
return res;
}
}

Categories

Resources