Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm building a calculator app which take up math expressions, parses it and displays the results, for that I use Javaluator.
Something like:
String expression = "(2^3-1)";
Double result = new DoubleEvaluator().evaluate(expression);
I use two textView for this: one for displaying user input (expression) and the other for displaying the results which are a Double.
Everything works fine but I would like to get rid of the float that is returned after each operation: e.g: 10 * 10 = 100.0. I tried something like finResult = result.intValue(); works but is broken for division operations. e.g: 2 / 3 = 0.
Is there a way to fix that?
For getting rid of trailing zero, you can use DecimalFormat and its method setMinimumFractionDigits(0).
DecimalFormat dc = new DecimalFormat();
dc.setMinimumFractionDigits(0);
String finResult = dc.format(result);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm a beginner at java and I've been trying to make a calculator using Swing. So far everything is good, but I'm having trouble with how the number pad will work. I want the calculator's output to be a float value, but I want it to be so that if you click "1", the output will display "1", not "1.00". How could I go about this?
Also, I cannot think of a way to append a number to another number. For example, if I input 1 then input 2, the output would be 1.02, not 12. How do I get the program to make the output be a whole number when possible?
For the first question: You first want to check that there are no digits after the decimal point when just displaying 1 instead of 1.00. To do so you can check that this actually is the case with this if statement:
if(number % 1.0f == 0)
next, you want to convert this number into a string that just displays the number without the decimal points, to do so you can cast number to an int and then convert it into a string with:
String.valueOf((int) number)
For the second question: To append a new digit to the number I would suggest generating a string with the number the user enters using StringBuilder. In practice it would look something like:
StringBuilder sb = new StringBuilder();
// when user presses "2" button for example:
sb.append("2");
In the end you want to convert this generated String into a float in order to perform calculations on it, which you can do with:
float value = Float.valueOf(sb.toString());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If you prompt the user for a price and they input $25.50, how would I take out the numbers and store them as a Double so that I can use them in equations.
I have tried,
System.out.println("What is the price of the product(e.g. 35.21)?");
price = scanner.nextLine();
price_2 = price.replaceAll("[^0-9]", "");
price_3 = Double.parseDouble(price_2);
System.out.println(price_3);
But it does not maintain the decimal.
You can use a regex to strip out all non-numeric characters from the String, while leaving the decimal point.
Then, just parse the string to a double:
String str = "$25.50";
double dbl = Double.parseDouble(str.replaceAll("[^0-9,.]",""));
System.out.println(dbl);
In your code, you were removing the decimal point as well. Simply add the ,. to your regex.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code :
variables : Double meter, km ;
it has to convert Km to meters and pass the value to the text fiels in this case "tfm"
meter=km*1000;
Double.parseDouble(tfm.setText(meter));
However it show error which says:
incompatible types : double cannot be converted to string
Does anybody knows how to fix it ?
Thank you in Advance
To set any value to label :
label.setText(YourValue);
so , in your case use this line:
tfm.setText(meter+"");
meter = km * 1000;
is correct. However, in your line of
Double.parseDouble(tfm.setText(meter));
You have several problems:
setText expects String and is void, therefore you cannot use its value, as seen here. You try to use this void as a parameter at parseDouble, which expects String, seen here. You should have something like:
tfm.setText(meter + "");
instead.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a function:
ItemValue[i] = substring(toString(obj),0,toString(obj).indexOf(".") + 4);
where obj is a number. The function works for 9,999,999.99999 -> 9999999.999 but values such as 99,999,999.99999 gets converted to 9.999.
Is it a data type issue?
Thanks,
The problem is with the double values you are using. The value 99999999.99999 will be translated to 9.999999999999E7 by toString() so the results you are getting. In order to strip the digits after decimal you can use DecimalFormat class:
DecimalFormat f = new DecimalFormat();
f.setMaximumFractionDigits(3);
f.setMinimumFractionDigits(3);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have some line of Java code like this:
Ball b = new Ball(Util.toPx(X*mR), y);
and I would like to replace this line with this
Ball b = new Ball(X*mR, y);
where X is a double number like 1.25, 1.765 etc.
How to obtain this with Eclipse search-replace feature?
I don't know about eclipse, but on sublime text 2 you can obtain it with the search feature with regex enabled. The regex would be something like:
Util\.toPx\((.*?)\)
And if you want to replace this String for something else you could do:
toPx($1)
It would replace the previous string to
toPx(X*mR)
If you have more groups between parentheses, you can use them on the replace expression with $1, $2, etc.