Conversion calculation from string do double [duplicate] - java

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
i'm working in android and I need your help. In string in values I have this
<item>(1 / 1024)</item>
I need to parse this string to double this way
outputDouble = Double.parseDouble(unitsValues[outputPosition]);
so it means this
outputDouble = Double.parseDouble((1 / 1024));
This second code mean, that I find the line by index I need and it tries to convert it from string to double but it is impossible because it can recognize this string (1 / 1024) to double. Do you have any ideas?
Thank you

There is no built in method to do that ... However you could do that with an external library like BeanShell :
Interpreter interpreter = new Interpreter();
interpreter.eval("(1 / 1024)");
To use Beanshell with Android, download the bsh-core.jar file, put it in a /lib folder in your project, and adjust your Eclipse settings or Ant script to reference that JAR during compilation and packaging.

Maybe this will illustrate you how to do the parsing using the String#split. There is no direct method to do the conversion.
String representation = getString(R.string.my_repr);
String [] splits = representation.split("/");
Double num = Double.valueOf(splits[0].trim()) / Double.valueOf(splits[1].trim());
EDIT I also added trimming to the strings, because of the danger of spurious interval symbols.

Using regex parse (1 / 1024) into two Double values 1f and 1024f and then divide them to get a Double value.

Related

How can you display 1 as 01 in java? [duplicate]

This question already has answers here:
Right padding with zeros in Java
(7 answers)
Closed 2 years ago.
How can I display 1 as 01 in Java when using the g.drawstring command I have tried to look but I don't know what term I should use.
You can format String and display it like this:
g.drawString(String.format("%02d", 1));
%02d is used for formatting, where 02 states for two digits with leading zero as necessary, and d - decimal number
Source: Official Oracle Documentation on formatting numeric strings
You could use Java's String.format() method, like this (recommended)
String.format("%02d", num)
Or if you could write something like this:
String text = (num < 10 ? "0" : "") + num;
If you want to print a string that contains that number, you can use String.format.
If you write something like String.format("%02d", yourNumber), for yourNumber=1 you will obtain the string 01, so you can use the previous code in a System.out or draw it on screen.
If you want to use g.drawstring, you can use the following code:
g.drawString(String.format("%02d", yourNumber), x, y)

How do i convert a string to double in java when the string contains math functions? [duplicate]

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
i have a string with a math function, like "35+20". i want a new double variable that takes in the result of the math function i.e, 55.0 How do i achieve this? this is actually for android, i'm making a calculator..
Manually parse the string and do a calculation at each operator symbol. It will get more complicated when dealing with brackets, however.
If you want to write it yourself, you'll probably want to implement the Shunting Yard Algorithm.
There are also some libraries that handle it for you.
https://github.com/uklimaschewski/EvalEx
Since you have mentioned you are working on a calculator I am assuming that you might not only be interested in just the + operation but on a bunch of other stuffs too.
You can look in to open source GitHub project linked below which provides the JAVA implementation for the stuff you are trying to do https://github.com/uklimaschewski/EvalEx
which can give you a good set of functionality that you desire.
This project takes in a string as an expression and the returns the result in BigDecimal format.
You can always extend it and tweek it to custom suite you needs.

why float and double showing different behaviour in java? [duplicate]

This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
case 1
float a=033.0 //shows compilation problem
case 2
double a=033.0 //works fine
Why case 1 is showing error but not case 2 or vice-versa?
By default java uses double type to store floating values. since you are storing double in float (down casting) java will throw an error. it can be resolved by two ways
float a=033.0f
float a= (float)033.0
case 1----float a=033.0 //shows compilation problem
case 2----double a=033.0 //works fine
In Java, decimal number is interpreted as a double ,so converting from double to float cannot be performed automatically ,so you need to give like this :
float a= 033.0f;
Its simply the understanding of Java Syntax.
You can read the Primitive data types of java.
You'll get it anywhere...
Link : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Direct to this link, do ctrl+F, & paste this "Floating-Point Literals".
You wont't waste time wandering...
For your own convenience now, you can prefer this.
float fractionNumber = 25.24F;
fractionNumber = 25.24f;
double biggerFractionNum = 56.65555D;
biggerFractionNum = 56.65555;
but generally its like this all over... later you'll get used to it.
float foo = 34.4F;
double doo = 34.4;
IMPLEMENT it right away ! ...experience it, understand it...& you'll never forget it. :)

variable/parameterized width for printf/format in Java (using * or something else?) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java printf using variable field size?
I haven't worked with Java in a while so I was looking for a way to
specify variable width in format/printf when formatting/printing output. My example shows the use with an integer, but of course I'd like this to work for other types too.
E.g., something along the lines of
int val = 8;
int wid = 5;
System.out.printf("%"*d\n", wid, val);
I could use this work-around, which is ugly:
System.out.printf("%"+wid+"d\n", val);
Was the * variable field width specifier removed from Java? This old'ish
page, section 1.3.1,
shows the use (like it would be used in C), but I can't get it to
work, resulting in:
java.util.UnknownFormatConversionException: Conversion = '*'
nor have I been able to find more recent references that this
does work.
Is there an easier way to do this other than my work-around above?
I did look around before posting and came across this about 2-year old SO question Java printf using variable field size? but is that the final word on this?
The general syntax of a format specifier is
%[parameter][flags][width][.precision][length]type
Instead of printf, you can also use
String.format("%"+wid+"d",val);
And yes, these are the only ways in case you are using a Java Formatter.

Script groovy tag not working properly in Play

Hey guys I am using the script tag.
%{..}%
When I do multiplication it seems to work.
up = wrapBenchmark * upperLimit;
But if I do this.
up = wrapBenchmark + upperLimit;
It seems to add the number as a string to the end. Like a string concat. What is the issue here? I just want to add two numbers together. It's treating everything as a string. Thanks for the help.
Standard String class (java) has overloaded operator + (string concat), and no overloaded operator * (multiply). So, interpreter casts variables to an integer, when cannot invoke multiply on string objects. And groovy is a dynamically typed language, so parameters sends like strings.
To solve your issue write this
up = wrapBenchmark.toInteger() + upperLimit.toInteger();

Categories