How to print a large number with exponential notation? - java

If I have a number 52000000000, and I want to print it out as 5.2E+9, how do I do that with printf?

Since it's a simple method I'll guide you through it.
You'll need two counters: one for the number itself (type double, let's call it 'num'), and one for the exponent (integer, let's call it exp). You'll need a while loop such that while num is greater than 10, divide num by 10 and increase exp by 1. So, something like 7600 should have num = 7.6, exp = 3.
Depending on your return type, you can return these values in numerous ways. A simple way would be to have return type string and return num+"E"+exp.

This can be done with printf("%.2g", 52000000000);
"g" is the conversion character for scientific notation for formatting, and ".2" specifies the precision.

You need to have a look at this : DecimalFormat
formatter = new DecimalFormat("0.#E0");
System.out.println(formatter.format(value));//value is where you store the number to be formatted

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

Fastest way to generate a random double number in java

In a project in which I'm working for, I'm required to either generate n random double numbers (depending from what the input file says) or converting them to doubles if I have them from my input file. Those numbers should have only 2 decimals after the comma (ex.: 0.98).
I know that in Java 8, there are 2 ways of doing this:
Java 8 way: nthNumber = Double.parseDouble(new DecimalFormat("#.##").format(ThreadLocalRandom.current().nextDouble(0,1)).replace(",","."));
Old fashion way: nthNumber = Double.parseDouble(new DecimalFormat("#.##").format(new Random().nextDouble()).replace(",", "."));
Asymptotically speaking, which is the fastest? From my poor knowledge of A.D.S., I'd say it would be the same time (O(n)?) but I'm not 100% sure
Aside from these two ways, are there any other ways to generate random doubles between 0 and 1 which are faster, asymptotically speaking, than my proposals? (rather, are there methods that can do everything in O(n) or O(1) ?)
Thank you in advance to everyone who will spend a minute to answer to this question of mine
Both of your approaches use strings as an intermediate representation, this will be quite inefficient (memory allocation, string parsing, string formatting are all relatively slow operations. You probably also want to avoid allocating multiple instances of Random.
Given that you only want two decimal digits, why not create an integer in the range of 0..99 and divide it by 100.0?
Random random = new Random(); // Only one instance needed.
for (int n = 0; n < total; n++) {
double nthRandomNumber = random.nextInt(100) / 100.0;
}
Your code looks complicated.
Did you consider the following:
DecimalFormat decimalFormat = new DecimalFormat("0.00");
String twoDigitRandom = decimalFormat.format(Math.random());
Reference:
https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Edit: Added after the comment:
If you would like to control the number of digits and not to have a String as end result, then I would advise not to use Double, try BigDecimal instead:
MathContext m = new MathContext(3);
BigDecimal randomWithTwoDigits = new BigDecimal(Math.random(), m);

How to solve "operator * cannot be applied to java.lang.string"?

I have been trying to convert value to decimal such as RM108.00. I have used
the method value = String.format("%.2f", curProduct.price); and it works,
but when i want to
apply value * ShoppingCartActivity.getProductQuantity(curProduct), it says
that operator * cannot applied to java.lang.string. How do i solve this
problem? How should i make the total price into 2 decimal?
item.productPrice = (TextView) convertView
.findViewById(R.id.textViewTotal);
value = String.format("%.2f", curProduct.price);
if (mShowPrice) {
item.productPrice.setText("Total Price: " +
String.valueOf(curProduct.price *
ShoppingCartActivity.getProductQuantity(curProduct)));
} else{
item.productPrice.setText("RM" + String.valueOf(value));
}
What this means is that one of the two following methods is a string, not a number:
curProduct.price
ShoppingCartActivity.getProductQuantity(curProduct)
You can convert them to an integer (Or float, if there is a decimal in it) by using the following code:
Integer.parseInt(curProduct.price);
Float.parseFloat(curProduct.price);
It might also be worth looking at where these value are defined, and making sure to use integers/floats whenever possible, as the conversions can take some time. Integers should always be used for whole numbers (If they aren't extremely large), and floats or doubles should be used for decimal numbers. Don't use strings for numbers unless you know exactly what you are doing!
Thats because String.format() gives you a formatted String and not any other formatted datatype you expect, and you cannot operate arithmetic operators (such as +) on Strings. Why don't you try to parse all strings to datatypes by using
Double.parseDouble(value) for double
Float.parseFloat(value) for float
Integer.parseInt(value) for int
and then perform the arithmetic operations.

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

How to shift 4 decimal places in a number using DecimalFormat

I am trying to convert $11,120.6 million to 1.12 billion. My question is how to shift by 4 decimal places every time after the decimal point since I will be inserting values dynamically in excel.
Original title was a bit misleading, so my answer provides more than one way of accomplishing this task. First part reflects the answer to the new title.
11,120.6 shifted by four decimal places to the left will not create 1.12; more like 1.112 or 1.11 (depending on the number of decimal places). But anyways, here are some thoughts:
DecimalFormat way:
float num = 11120.6;
DecimalFormat numFormat = new DecimalFormat("#.00");
System.out.println(numFormat.format(num));
If it's purely visual, you can just do something like this:
float num = 11120.6;
System.out.println(String.format("%.2f", num));
That one will give you two decimal places to the right of the decimal point, producing 1.11.
Or how about something like this?
float num = 11120.6;
num /= 10000.0;
This will give you 1.11206.
You can create if else logic or something similar to divide/multiple by an appropriate amount, based on the number, i.e.:
if (num >= 10)
{
num /= 10.0;
}
else if (num >= 100)
{
num /= 100.0;
}
...etc...
You can also do something like this along with the similar logic to above:
BigDecimal num = new BigDecimal("11120.6");
num = num.movePointRight(-4);
Unless someone can think of a better way you'll also need to know that the number before was in millions representation and the new number is in billions. Store it in a separate variable as an enumeration, perhaps.
Something else you can do, but it's not very efficient, is what we did in my C++ class in college when we were messing around. You can create several arrays or lists (depending on what it is that you do) for certain number sizes. When you want to display a number of say... listBillions, you'd divide any number from that list by 1000000000.0, or format it as a string. So, in the the end you have your lists of dozens, hundreds, thousands, etc., and you'll always know that listBillions[i] can be nothing but a billion in whatever form it is being displayed.

Categories