Multiplying string by a double - java

I have code like this:
double priceMulti = 1.2;
double price = Double.parseDouble(jTextField1.getText());
double date = 1980;
double random = Math.random()*20;
jLabel28.setText(priceMulti*String.valueOf(price)*date*random);
and in line with setting text I got an error "bad operand types for binary operator "*""
so I cant multiple anything.
Edit: main question is Solved, but now I want to use BigDecimal, not Doubles, because they are like 1.000012, and I dunno how.

You're trying to multiply a String value with a double value. The arithmetic operators do not work on String values. You need to multiply all the doubles and then get the String value of it to set it to your jLabel28.
jLabel28.setText(String.valueOf(priceMulti * price * date * random));

You cannot multiply string by double in Java. You should do multiplication of all the doubles you have and then cast it to string to set the resulting value into Label text

Related

How can I assign value of .2f value of one variable to another variable?

I want to assign a double value to another variable but only until it's first 2 decimal value.
Suppose I have 3.4586748547 value in a variable but I want to save 3.45 in another variable. How can I do this?
-I am using JAVA
Using BigDecimal (import java.math.BigDecimal;):
BigDecimal input = new BigDecimal(String.valueOf(3.4586748547d));
double output = input.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();
System.out.println(output);
You can also use regex for that purpose, like below:
String inputString = String.valueOf(3.4586748547d);
String outputString = inputString.replaceAll("(\\d+\\.\\d{2})\\d+", "$1");
System.out.println(outputString);
The fixed point type in Java is BigDecimal.
Floating point is just an approximation, a sum of powers of 2. 3.45 or 3.4499999987 might be the exact same double. But one can round, and when printing with %.2f format will be shown nice. Though the error will be shown when summing or multiplying.
x = Math.round(x * 100) / 100.0;
Unfortunately.

Converting String/Double to Int in Android

Hello new to android/Java,
I am using JSON to Parse values as strings and doubles. I am getting strings/doubles such as "6503.04" or "12.3942" etc. I am looking to see if I can convert these strings into an integer and also doubles into integers. I just need to get rid of the decimals points in the easiest way possible. How can I make that happen?
Any help appreciated.
double x = Double.parseDouble(your_string);
int y = (int) x;
y is going to have value of x with decimals cutted of.
Before casting to int you are able to floor or ceil the number if you want.
Don't forget that parsing functions usually throw an exception when your_string is not a number.
//get the value from json as double then get the integer
Double number = jsonObject.getDouble('longitude')
int newNumber = number.intValue()

Conversion of BigDecimal to double

While converting BigDecimal to double, extra precisions are getting added.
Could you please suggest this.
For Ex:
BigDecimal deci= new BigDecimal(1.23456789123457E+17);
double value = deci.doubleValue();
System.out.println(value);
And value prints - 1.23456789123456992E17. How come the last digit 7 is converted to 6992? Is there is any way I can get the same input which I am passing to BigDecimal after converting to double?
There are useful methods Math.nextUp() and Math.nextDown() which can help you to investigate this issue:
BigDecimal deci= new BigDecimal(1.23456789123457E+17);
double value = deci.doubleValue();
System.out.println(value);
System.out.println(Math.nextDown(value));
System.out.println(Math.nextUp(value));
The output is:
1.23456789123456992E17
1.23456789123456976E17
1.23456789123457008E17
So as you can see, the double number ...456976 is immediately followed by ...456992, which in turn is immediately followed by ...457008. So BigDecimal.doubleValue() should select between two closest numbers ...456992 and ...457008. No other double number exist between them.
Actually you don't even need the BigDecimal for this test:
double value = 1.23456789123457E+17;
System.out.println(value); // prints 1.23456789123456992E17

How to return a double with two decimal places?

I want to return a double with 2 decimal places (i.e. 123.00). The following are part of the codes. The output is always 123.0. How to get a two decimal places?
public static double getPrice(){
double price = Double.valueOf(showInputDialog("Stock's price : "));
DecimalFormat rounded = new DecimalFormat("###.00");
double newPrice = Double.valueOf(rounded.format(price));
System.out.println(newPrice);
return newPrice;
}
As long as the return type of your function is double, the short answer is "you can't".
Many numbers that have no more than two decimal digits cannot be represented exactly as double. One common example is 0.1. The double that's nearest to it is 0.100000000000000005551115...
No matter how much rounding you do, you wouldn't be able to get exactly 0.1.
As far as your options go, you could:
accept the rounding issues associated with using double;
return an int (i.e. the rounded value multiplied by 100);
return a String;
return a BigDecimal.
In particular, if you're representing monetary amounts, using double is almost certainly a bad idea.
When formatting, you're making a string from a double. Don't convert your formatted string to a double again :
String formattedPrice = rounded.format(price);
System.out.println(formattedPrice); // prints 123.00 or 123,00, depending on your locale
A double keeps a numerical value following IEEE754, it doesn't keep any formatting information and it's only as precise as the IEEE754 double precision allows. If you need to keep a rendering information like the number of digits after the comma, you need something else, like BigDecimal.

Converting double to integer in Java

In Java, I want to convert a double to an integer, I know if you do this:
double x = 1.5;
int y = (int)x;
you get y=1. If you do this:
int y = (int)Math.round(x);
You'll likely get 2. However, I am wondering: since double representations of integers sometimes look like 1.9999999998 or something, is there a possibility that casting a double created via Math.round() will still result in a truncated down number, rather than the rounded number we are looking for (i.e.: 1 instead of 2 in the code as represented) ?
(and yes, I do mean it as such: Is there any value for x, where y will show a result that is a truncated rather than a rounded representation of x?)
If so: Is there a better way to make a double into a rounded int without running the risk of truncation?
Figured something: Math.round(x) returns a long, not a double. Hence: it is impossible for Math.round() to return a number looking like 3.9999998. Therefore, int(Math.round()) will never need to truncate anything and will always work.
is there a possibility that casting a double created via Math.round() will still result in a truncated down number
No, round() will always round your double to the correct value, and then, it will be cast to an long which will truncate any decimal places. But after rounding, there will not be any fractional parts remaining.
Here are the docs from Math.round(double):
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
For the datatype Double to int, you can use the following:
Double double = 5.00;
int integer = double.intValue();
Double perValue = 96.57;
int roundVal= (int) Math.round(perValue);
Solved my purpose.

Categories

Resources