I have below piece of code and my precision is getting lost while converting BigDecimal to String. Any help on this really appreciated.
Expected result : 95.10000000000000%
Actual Result : 95.09999999999999%
public static String getAsString(Object value)
{
if (value == null)
return null;
if (value.toString().length() == 0)
return null;
BigDecimal inputVal = new BigDecimal(value.toString());
if (inputVal == new BigDecimal(0))
return "";
NumberFormat numberFormat = NumberFormat.getPercentInstance(Locale.US);
numberFormat.setMinimumFractionDigits(14);
numberFormat.setMaximumFractionDigits(14);
if (numberFormat instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) numberFormat;
df.setNegativePrefix("(");
df.setNegativeSuffix("%)");
}
String num = null;
num = numberFormat.format(new BigDecimal(95.1).divide(new BigDecimal(100)));
return num;
}
With this instance creation
new BigDecimal(95.1)
you are losing some precision. What you actually can do is to use the string constructor:
new BigDecimal("95.1")
Another way for your case is to use the factory methods:
BigDecimal.valueOf(95.1)
BigDecimal.valueOf(100)
Internally these use the string representation of those numbers to create a BigDecimal object.
In this case using the new BigDecimal(double x) and double as an argument and it happens that doubles can't represent x.xxxx exactly. So x.xxxx is "transformed" to the closest possible double, which is x.0000xxxxxxxxxxxzzzzzzzzzzyyyyy and that's what your BigDecimal shows.
So, here the solution could be you can use the String constructor or the valueOf() method, to create the canonical represent of your doubled value.
By changing your line
num = numberFormat.format(new BigDecimal(95.1).divide(new BigDecimal(100)));
to
num = numberFormat.format(BigDecimal.valueOf(95.1).divide(BigDecimal.valueOf(100)));
or
num = numberFormat.format(new BigDecimal("95.1").divide(new BigDecimal("100")));
should give you the correct result.
Try this
BigDecimal inputVal = new BigDecimal(value.toString()).setScale(3, RoundingMode.HALF_UP);
You can use setScale and then convert to a String.
I see you want to print 14 digits, so set the scale to 14, and use rounding up.
BigDecimal inputVal = new BigDecimal(95.10000000000000).setScale(14, RoundingMode.UP);
Then call toString and add the % etc.
You can use string value
num = numberFormat.format(new BigDecimal("95.1").divide(new BigDecimal("100")));
Related
I am invoking a method called "calculateStampDuty", which will return the
amount of stamp duty to be paid on a property. The percentage calculation works
fine, and returns the correct value of "15000.0". However, I want to display the value to
the front end user as just "15000", so just want to remove the decimal and any preceding values
thereafter. How can this be done? My code is below:
float HouseValue = 150000;
double percentageValue;
percentageValue = calculateStampDuty(10, HouseValue);
private double calculateStampDuty(int PercentageIn, double HouseValueIn){
double test = PercentageIn * HouseValueIn / 100;
return test;
}
I have tried the following:
Creating a new string which will convert the double value to a string, as per below:
String newValue = percentageValue.toString();
I have tried using the 'valueOf' method on the String object, as per below:
String total2 = String.valueOf(percentageValue);
However, I just cannot get a value with no decimal places. Does anyone know
in this example how you would get "15000" instead of "15000.0"?
Thanks
Nice and simple. Add this snippet in whatever you're outputting to:
String.format("%.0f", percentageValue)
You can convert the double value into a int value.
int x = (int) y where y is your double variable. Then, printing x does not give decimal places (15000 instead of 15000.0).
I did this to remove the decimal places from the double value
new DecimalFormat("#").format(100.0);
The output of the above is
100
You could use
String newValue = Integer.toString((int)percentageValue);
Or
String newValue = Double.toString(Math.floor(percentageValue));
You can convert double,float variables to integer in a single line of code using explicit type casting.
float x = 3.05
int y = (int) x;
System.out.println(y);
The output will be 3
I would try this:
String numWihoutDecimal = String.valueOf(percentageValue).split("\\.")[0];
I've tested this and it works so then it's just convert from this string to whatever type of number or whatever variable you want. You could do something like this.
int num = Integer.parseInt(String.valueOf(percentageValue).split("\\.")[0]);
Try this you will get a string from the format method.
DecimalFormat df = new DecimalFormat("##0");
df.format((Math.round(doubleValue * 100.0) / 100.0));
Double d = 1000d;
System.out.println("Normal value :"+d);
System.out.println("Without decimal points :"+d.longValue());
Use
Math.Round(double);
I have used it myself. It actually rounds off the decimal places.
d = 19.82;
ans = Math.round(d);
System.out.println(ans);
// Output : 20
d = 19.33;
ans = Math.round(d);
System.out.println(ans);
// Output : 19
Hope it Helps :-)
the simple way to remove
new java.text.DecimalFormat("#").format(value)
The solution is by using DecimalFormat class. This class provides a lot of functionality to format a number.
To get a double value as string with no decimals use the code below.
DecimalFormat decimalFormat = new DecimalFormat(".");
decimalFormat.setGroupingUsed(false);
decimalFormat.setDecimalSeparatorAlwaysShown(false);
String year = decimalFormat.format(32024.2345D);
With a cast. You're basically telling the compiler "I know that I'll lose information with this, but it's okay". And then you convert the casted integer into a string to display it.
String newValue = ((int) percentageValue).toString();
You can use DecimalFormat, but please also note that it is not a good idea to use double in these situations, rather use BigDecimal
String truncatedValue = String.format("%f", percentageValue).split("\\.")[0]; solves the purpose
The problem is two fold-
To retain the integral (mathematical integer) part of the double. Hence can't typecast (int) percentageValue
Truncate (and not round) the decimal part. Hence can't use String.format("%.0f", percentageValue) or new java.text.DecimalFormat("#").format(percentageValue) as both of these round the decimal part.
Type casting to integer may create problem but even long type can not hold every bit of double after narrowing down to decimal places. If you know your values will never exceed Long.MAX_VALUE value, this might be a clean solution.
So use the following with the above known risk.
double mValue = 1234567890.123456;
long mStrippedValue = new Double(mValue).longValue();
Alternatively, you can use the method int integerValue = (int)Math.round(double a);
Double i = Double.parseDouble("String with double value");
Log.i(tag, "display double " + i);
try {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0); // set as you need
String myStringmax = nf.format(i);
String result = myStringmax.replaceAll("[-+.^:,]", "");
Double i = Double.parseDouble(result);
int max = Integer.parseInt(result);
} catch (Exception e) {
System.out.println("ex=" + e);
}
declare a double value and convert to long convert to string and formated to float the double value finally replace all the value like 123456789,0000 to 123456789
Double value = double value ;
Long longValue = value.longValue();
String strCellValue1 = new String(longValue.toString().format("%f",value).replaceAll("\\,?0*$", ""));
public class RemoveDecimalPoint{
public static void main(String []args){
System.out.println(""+ removePoint(250022005.60));
}
public static String removePoint(double number) {
long x = (long) number;
return x+"";
}
}
This should do the trick.
System.out.println(percentageValue.split("\\.")[0]);
Try:
String newValue = String.format("%d", (int)d);
I am trying to convert float number in Java to integer on the following way:
4.55 = 455
12.45 = 1245
11.1234 = 111234
How can I do it?
One option would be like this:
float number = 4.55f;
int desiredNumber = Integer.parseInt(String.valueOf(number).replaceAll("\\.", ""));
But something like this will only work if the conversion pattern will stay the same. By this I mean the way you want to convert from float to int. Hope this helps.
here is an example
double f1 = 4.5;
String str = new Double(f1).toString();
str = str.replace(".", "");
Integer i = Integer.parseInt(str);
System.out.println(i);
If you want to be able to hanlde arbitrarily large numbers and arbitrarily many decimals, then you can use BigDecimal and BigInteger
BigDecimal number = new BigDecimal(
"5464894984546489498454648949845464894984546489498454648949845464894984546489498454648949845464894984.1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
String valueOf = number.toPlainString();
BigInteger desired = new BigInteger((valueOf.replaceAll("\\.", "")));
System.out.println(desired);
Constructor can take double or float if needed
BigDecimal number = new BigDecimal(Double.MAX_VALUE);
BigDecimal number = new BigDecimal(Float.MAX_VALUE);
Something like that :
public int convert( float numbre) {
String nmbre = String.valueOf(numbre).replace(".", "");
return Integer.parseInt(nmbre );
}
You can convert the number to a String, remove the dot, and create a new Long:
private long removeTheDot(Number number) {
return Long.valueOf(number.toString().replace(".", ""));
}
Ka-Bam!
I need to remove the fractional part of a BigDecimal value when its scale has a value of zero. For example,
BigDecimal value = new BigDecimal("12.00").setScale(2, RoundingMode.HALF_UP);
It would assign 12.00. I want it to assign only 12 to value in such cases.
BigDecimal value = new BigDecimal("12.000000").setScale(2, RoundingMode.HALF_UP);
should assign 12,
BigDecimal value = new BigDecimal("12.0001").setScale(2, RoundingMode.HALF_UP);
should assign 12.
BigDecimal value = new BigDecimal("12.0051").setScale(2, RoundingMode.HALF_UP);
should assign12.01
BigDecimal value = new BigDecimal("00.000").setScale(2, RoundingMode.HALF_UP);
should assign 0.
BigDecimal value = new BigDecimal("12.3456").setScale(2, RoundingMode.HALF_UP);
should assign 12.35 and alike. Is this possible? What is the best way to do?
For the crosslink from there: https://codereview.stackexchange.com/questions/24299/is-this-the-way-of-truncating-the-fractional-part-of-a-bigdecimal-when-its-scale
Is this possible? What is the best way to do?
Probably stripTrailingZeros().
To check your tests:
public static void main(final String[] args) {
check(truncate("12.000000"), "12");
check(truncate("12.0001"), "12");
check(truncate("12.0051"), "12.01");
check(truncate("12.99"), "12.99");
check(truncate("12.999"), "13");
check(truncate("12.3456"), "12.35");
System.out.println("if we see this message without exceptions, everything is ok");
}
private static BigDecimal truncate(final String text) {
BigDecimal bigDecimal = new BigDecimal(text);
if (bigDecimal.scale() > 2)
bigDecimal = new BigDecimal(text).setScale(2, RoundingMode.HALF_UP);
return bigDecimal.stripTrailingZeros();
}
private static void check(final BigDecimal bigDecimal, final String string) {
if (!bigDecimal.toString().equals(string))
throw new IllegalStateException("not equal: " + bigDecimal + " and " + string);
}
output:
if we see this message without exceptions, everything is ok
The following code does what you specify with the help of regex and the construction of BigDecimal, it is probably not the most efficient way to go about:
BigDecimal value = new BigDecimal("12.0000").setScale(2, RoundingMode.HALF_UP);
Pattern pattern = Pattern.compile("\\d+\\.(\\d)0");
Matcher matcher = pattern.matcher(value.toString());
if (matcher.find()) {
if (matcher.group(1).equals("0"))
value = value.setScale(0);
else
value = value.setScale(1);
}
Note: this also makes 12.100 -> 12.1, I assume that is wanted functionality.
Instead of modifying the original value on my own by hand, I have simply tried to apply some conditions like,
String text="123.008";
BigDecimal bigDecimal=new BigDecimal(text);
if(bigDecimal.scale()>2)
{
bigDecimal=new BigDecimal(text).setScale(2, RoundingMode.HALF_UP);
}
if(bigDecimal.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO)==0)
{
bigDecimal=new BigDecimal(text).setScale(0, BigDecimal.ROUND_HALF_UP);
}
System.out.println("bigDecimal = "+bigDecimal);
The first if condition checks, if the scale is greater than 2. If so, then perform the rounding operation as specified.
The next if condition checks, if the value of the fractional part yields zero. If so, then truncate it. Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by the compareTo() method. Therefore, the condition is satisfied whenever the the value of the fractional part is evaluated to zero.
Otherwise, the original value is left untouched (if the scale is either 1 or 2).
If I'm missing something or it's a wrong thing to implement, then please clarify it.
Try this one:
new BigDecimal("12.3456").setScale(2, RoundingMode.FLOOR).stripTrailingZeros()
I have a BigDecimal object and i want to convert it to string.
The problem is that my value got fraction and i get a huge number (in length) and i only need the original number in string for example:
for
BigDecimal bd = new BigDecimal(10.0001)
System.out.println(bd.toString());
System.out.println(bd.toPlainString());
the output is:
10.000099999999999766941982670687139034271240234375
10.000099999999999766941982670687139034271240234375
and i need the out put to be exactly the number 10.0001 in string
To get exactly 10.0001 you need to use the String constructor or valueOf (which constructs a BigDecimal based on the canonical representation of the double):
BigDecimal bd = new BigDecimal("10.0001");
System.out.println(bd.toString()); // prints 10.0001
//or alternatively
BigDecimal bd = BigDecimal.valueOf(10.0001);
System.out.println(bd.toString()); // prints 10.0001
The problem with new BigDecimal(10.0001) is that the argument is a double and it happens that doubles can't represent 10.0001 exactly. So 10.0001 is "transformed" to the closest possible double, which is 10.000099999999999766941982670687139034271240234375 and that's what your BigDecimal shows.
For that reason, it rarely makes sense to use the double constructor.
You can read more about it here, Moving decimal places over in a double
Your BigDecimal doesn't contain the number 10.0001, because you initialized it with a double, and the double didn't quite contain the number you thought it did. (This is the whole point of BigDecimal.)
If you use the string-based constructor instead:
BigDecimal bd = new BigDecimal("10.0001");
...then it will actually contain the number you expect.
For better support different locales use this way:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
df.format(bigDecimal);
also you can customize it:
DecimalFormat df = new DecimalFormat("###,###,###");
df.format(bigDecimal);
By using below method you can convert java.math.BigDecimal to String.
BigDecimal bigDecimal = new BigDecimal("10.0001");
String bigDecimalString = String.valueOf(bigDecimal.doubleValue());
System.out.println("bigDecimal value in String: "+bigDecimalString);
Output:
bigDecimal value in String: 10.0001
// Convert BigDecimal number To String by using below method //
public static String RemoveTrailingZeros(BigDecimal tempDecimal)
{
tempDecimal = tempDecimal.stripTrailingZeros();
String tempString = tempDecimal.toPlainString();
return tempString;
}
// Recall RemoveTrailingZeros
BigDecimal output = new BigDecimal(0);
String str = RemoveTrailingZeros(output);
If you just need to set precision quantity and round the value, the right way to do this is use it's own object for this.
BigDecimal value = new BigDecimal("10.0001");
value = value.setScale(4, RoundingMode.HALF_UP);
System.out.println(value); //the return should be "10.0001"
One of the pillars of Oriented Object Programming (OOP) is "encapsulation", this pillar also says that an object should deal with it's own operations, like in this way:
The BigDecimal can not be a double.
you can use Int number.
if you want to display exactly own number, you can use the String constructor of BigDecimal .
like this:
BigDecimal bd1 = new BigDecimal("10.0001");
now, you can display bd1 as 10.0001
So simple.
GOOD LUCK.
To archive the necessary result with double constructor you need to round the BigDecimal before convert it to String e.g.
new java.math.BigDecimal(10.0001).round(new java.math.MathContext(6, java.math.RoundingMode.HALF_UP)).toString()
will print the "10.0001"
Please help me to solve this. I trying to get value from textview and stored as string. Then it converts to double. While converting up to 7 characters functioning normally but if i try to add more than 7 result is 1.23456789E8. Here is my code
String value = tvInput.getText().toString();
\\tvInput is my textView
Double result = 0.0;
Double input1=0.0;
Double input2=0.0;
input=Double.parseDouble(value);
result = input1 + input2;
tvInput.setText(Double.toString(result));
if i give input1 value as 1234567 and input2 as 1234567 i am getting correct result but if give input1 as 12345678 and input2 as 3. the output is 1.2345681E7
The value you get is correct, the issue is with the way you print it.
You're relying on toString for a double output; if you want to guarantee not to have an exponential notation, you should format it using a DecimalFormat, or with String.format;
DecimalFormat myFormatter = new DecimalFormat("############");
tvInput.setText(myFormatter.format(result));
Also see the format documentation
The behavior you describe is consistent with the javadoc. You could use String.format instead.
Either 12345678 and 1.2345678E7 are exactly the same number. No trouble with that
Your trouble is with the representation, if E>6 then toString() use scientific notation. You may want to use NumberFormat for this.
Use String.format: example
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String i1 = "12345678";
String i2 = "3";
double d1 = Double.parseDouble(i1);
double d2 = Double.parseDouble(i2);
double d = d1 + d2;
System.out.println( String.format("%f", d) );
}
}
Why don't use Integer instead?
String value = tvInput.getText().toString();
\\tvInput is my textView
int result = 0;
int input1 = 0;
int input2 = 0;
input=Integer.parseInt(value);
result = input1 + input2;
tvInput.setText(Integer.toString(result));