Format double as 4+ digit String - java

I have a double which contains values such as:
19.52
5.1
6.13
101.5
It will not contain more than 2 fractional digits. That is being checked for. If there are more than 2, the rest can be ignored.
I'd like to convert it to a String which has 4+ digits, like this:
"1952"
"0510"
"0613"
"10150"
How can I do that?
I'm using someone's API and the last 2 digits are specified as being always for the first 2 digits of the fractional part, that's why I need to do this.

You can multiply your number by 100 to remove the decimal point (because you only have 2 decimal digits), and then use the format 0000 to format it:
DecimalFormat df = new DecimalFormat("0000");
System.out.println(df.format(x * 100));
where x is your double.

Related

I am trying to divide a 19 digit number by 100 (19 digit number/100) in java

I am trying to divide a 19 digit number by 100, i.e. 19 digit number/100, in java. It can be divisible using long data type but I'm not getting the full value as 17 digits and a decimal point followed by another 2 digits. Instead, I'm only getting 17 digits as it was a long data type so I need that digit like mathematical expression.
long cardValue = ("1234567891234567891");
long divide = (cardValue/100);
System.out.println(divide);
Output: 12345678912345678
I need output as 12345678912345678.91
longs are large integers, and when you divide one by another you use integer division, which omits everything right of the decimal point.
You could use a BigDecimal instead:
BigDecimal divide = new BigDecimal(String.valueOf(cardValue)).divide(new BigDecimal(100));
Firstly, you are doing integer division, and then expecting a decimal value. In Java, 1234 / 10 results in 123, and not 123.4. If you want a decimal result, make one of the values decimal, i.e., 1234.0 / 10 or 1234 / 10.0. This will yield 123.4
As of your problem, since the number is very large, using BigDecimal is a better idea (not BigInteger, as it will again perform integer division, while you want a decimal result). So, try
BigDecimal b = new BigDecimal("1234567891234567891");
BigDecimal res = b.divide(new BigDecimal("100"));
Or you can do a one-liner as
new BigDecimal("1234567891234567891").divide(new BigDecimal("100"))
In first, res = 12345678912345678.91, and the other will also result in the same.
Note : Although BigInteger and BigDecimal are all included in java.math package, but if it raises an error, import it by using
import java.math.BigDecimal;
Integer and long don't have decimal point.
Instead, use double.
double a = 123456789.00
double answer = (a/100.00)
Refer primitive data types in java.
If you want to get the string and want to dive, then use parseDouble() method to convert it to double. After this step perform division.
The divide variable is long type, that means no decimal fractions, just integer values. If you use double, you can obtain decimals, but that type don't have the precision you need in this case because it gives up to 15 significant numbers.
Your only solution is to use BigDecimal class. You can obtain whatever digit numbers you want. We used it for Banks and accounting applications, and is easy to understand how to work with it.
HTH

Set n decimal places in BigDecimal and format the number Java

I am currently using the BigDecimal and with variable number of digits after decimal point. I need to be able to format the number with loosing the number of digits which I had set, because when I use format then it reduces the decimal digits to 2 - 4.
str1 =bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP);
str2 = NumberFormat.getInstance().format(bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP));
The first list works fine but when I want to format the String/BigDecimal then it drops the decimal places.
Note: Decimal place will vary from 0 to 15(from user). I am using Android Studio-API15/Java. Precision is important in my app, formatting is to improve readability.
As mentioned by #JB Nizet, you need to tune NumberFormat acc. to your need.
Below is a working example:
int numberOfDecimalPlaces = 6;
BigDecimal bigDecimal = new BigDecimal(11212.122323);
bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP);
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMinimumFractionDigits(numberOfDecimalPlaces);
System.out.println(numberFormat.format(bigDecimal));
Output:
11,212.122323

Convert double to double with one decimal point and one decimal place?

I want to round any double to a double with the format of one decimal point and one decimal place so 29575.347434 would be 2.3.
Tried doing this with decimalFormat but when i try #.# i just get a string in the format of 29575.3 with a , and i have no idea idea how to cut off all decimal points while keeping my value a double.
You could get the number as String, take the first digit and the digit after '.'. For non-negative numbers that would be
String s = Double.toString(29575.347434);
double d = Double.parseDouble(s.charAt(0) + "." + s.charAt(s.indexOf('.') + 1));
System.out.println(d); // 2.3
If the number can be negative, you would have to correct the code for the possible minus sign.
You could accomplish this by using the modulo operator %.
It looks like your decimal formatting is working with #.#. That will give you the whole number and one decimal place. You can then do your number 29575.3 % 10 and get 5.3.
Try this code and see:
System.out.println(29575.3d % 10);
Try the following:
Number(parseFloat(29575.347434).toFixed(1))
You could also pass a string , because parseFloat() would convert it to number, then trunkate to wished decimal (output is string again), then convert back to decimal by function Number().
Works for me.

How to use DecimalFormater in java?

i am new to the java, iam facing an issue in my task with DecimalFormat
when i give integer number it should not have any decimals,
if i give decimal number it should print decimal value
i used the DecimalFormater as below
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance();
decimalFormat.setMaximumFractionDigits(2);
decimalFormat.applyPattern("0");
System.out.println(decimalFormat.format(123456789.2569));
here it is printing without decimals.
i replace my patern with
decimalFormat.applyPattern("#.##");
in this case when i give 299
it is printing 3
but it should print 299
how to do it?
finally, if i give 265 it should be 265, if i give 265.36 it should be 265.36
If you use the #.## pattern you will get only the first digit and the rest of your number will be treated as fraction digits.
And that explains why you got 3 when you inputed 299 because with this pattern 299 is treated as 2.99 and then rounded to 3.
So try to use this pattern instead:
decimalFormat.applyPattern("###.##");
For further information about it you can take a look at:
Customizing Formats in Documentation
Java DecimalFormat | tutorials.jenkov.com

Format decimal values with String in Java

I am new to android and java programming, so please forgive me if this is an easy question. My problem is to add % in end of the decimal value. I will get double value and don't know how much digits after decimal point, i need to convert to two digits after decimal point and finally need to add % at last position. The problem is, I will format the double value only once. I have tried like this DecimalFormat ("#.##%"), but decimal points moved two digits forward. Please help me to get out of this problem.
Using DecimalFormat ("#.##%"),
Actual : 34.142545
Output : 3414.25%
If you use % in format, it means that you want to convert value to percentage, but you need to remember that value 1 in percentage world is equal to 100% so value you use will be automatically multiplied by 100. To avoid this behaviour you can change % into literal by quoting it with apostrophes '%'
new DecimalFormat("#.##'%'");
By adding the % in the format, you multiply by 100 and add the % character. This is why it looks like the decimal point moves.
You can use something like:
String output = new DecimalFormat("#.##").format(input) + "%";
An alternative is to divide by 100 before formatting:
String output = new DecimalFormat("#.##%").format(input / 100.0);

Categories