Could someone help with the following scenario.
In a textfield, i will give input say
String input1 = "120.3456";
But system will automatically take 2 decimal points and displays "120.35"
Now i will store using getAttribute("value") into a different string say
String getValue = driver.findElement(By.xpath("html/....xpath of the text field")).getAttribute("value");
How can i validate that my given input value is rounded to 2 decimal points in Selenium Webdriver?
Would be grateful if someone can provide me the best approach. Thanks in advance
Try as below :-
String input1="120.3456";
String getValue = driver.findElement(By.xpath("html/....xpath of the text field")).getAttribute("value");
String str = new BigDecimal(input1).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
return str.equals(getValue)
Hope it helps...:)
You can do it simply as below. You'll need to take the input and value strings and convert them to double. You'll need to round input and compare to value. This returns true if the two values are equal, false otherwise.
String input1 = "120.3456";
double input1Rounded = Math.round(Double.parseDouble(input1) * 100.0) / 100.0;
String value = driver.findElement(locator).getAttribute("value");
double valueRounded = Double.parseDouble(value);
return (input1Rounded == valueRounded);
Related
This question already has answers here:
java how to make user friendly percentage output from float number
(9 answers)
Closed 2 years ago.
I have a string value as below:
String percValue = "0.0209"
How can I convert this to something like as below
String formatedValue = "2.09%";
Can someone help me what is the simple and best way to convert this?
One good way would be to:
convert your percentage string to a number (needs to be a double type variable, so it can hold the decimal places...),
multiply the value by 100 to make it a percentage,
re-format the number in a string.
String percValue = "0.0209";
double percentage = Double.parseDouble(percValue) * 100;
String formattedValue = String.format("%.2f%%", percentage);
Explanation:
Double.parseDouble() takes your string as a parameter and returns a double value which you can do things like multiplication and addition with, and
String.format() lets you precisely control how your number is converted back to a String!
"%.2f" means "Take the next argument which is a floating-point variable and put it here, with two decimal places".
"%%" means "print a single '%'". You need two to "escape" it, since percent symbols are not literally interpreted in format strings.
You should parse the String into a double, multiply by 100, and then append the % sign to it as follows:
String percValue = "0.0209";
double per = Double.parseDouble(percValue);
String percentage = (per*100)+"%";
You need to parse your string value and then multiply by 100, something like this:
String percValue = "0.0209";
double value = ( Double.parseDouble(percValue)) * 100;
String formatedValue = value + "%";
Convert String to BigDecimal(for Big numbers) and multiply by 100.
String percValue = "0.0209";
BigDecimal temp=new BigDecimal(percValue).multiply(BigDecimal.valueOf(100)).stripTrailingZeros();
String formatedValue =temp.toString() + "%";
so I have an array list of Objects and in it are string numbers. I want to add decimal places to these numbers (8).
String value = String.valueOf(accountEntry.get(4));
double amount = Double.valueOf(value);
String formatted = String.format(Locale.GERMANY,"%.8f",amount);
accountEntry.add(formatted);
For example 101700000000 should output 1017 but instead it is 101700000000,00000000
Does anyone know where the problem is?
Hello try something like this using Regex , this way you can remove all zeros at the end.
String value = String.valueOf("101700000000");
double amount = Double.valueOf(value);
String formatted = String.format(Locale.GERMANY,"%d",(long)amount);
formatted = formatted.replaceAll("0+$", "");
System.out.println(formatted);
Input :101700000000 ===> Output: 1017
Does anyone know where the problem is?
Your Input is : 101700000000 and you are formatting String.format(Locale.GERMANY,"%.8f",amount); In here your output will be 101700000000,00000000 So for understand this String Format will not transform your input magicaly to 1017. You need to use another algorithm for this problem
This is what I have:
A string var amount with value: "123.45".
What I need is to divide it by 100 on a SpEL expression. So I went like this:
T(java.lang.Double).valueOf(amount) / 100D
And then, return the result as a string again.
How can I achieve it? I tried
T(java.lang.Double).toString(T(java.lang.Double).valueOf(amount) / 100D)
but is not working. And instead of dividing is multipling for some reason :-\
Thanks in advance
Consider, You have a variable named amount
String amount = "123.45";
//The following code converts 123.45 into Double and divides it by 100
Double dividedValue = new Double(Double.valueOf(amount)/100);
//To convert the resultant double value into String
String resultantValue = dividedValue.toString();
Hope, it will help you :)
Try converting the adjusted double to a string using String#valueOf:
T(java.lang.String).valueOf(T(java.lang.Double).valueOf(amount) / 100)
Here is a full explanation about convertion in double and back to String.
A possibility is
double myValue = 88.22;
String doubled = String.valueOf(myValue);
And the toString() method is a possibility to transfer it back.
I have a string "3,350,800" with multiple points I want to convert to double but have error multiple points
String number = "3,350,800"
number = number.replace(",", ".");
double value = Double.parseDouble(number);
Error : java.lang.NumberFormatException: multiple points
The . character is used as a decimal point in English, and you cannot have more than one of those in a number.
It seems like you're using it as a thousands separator though. This is legal in several locales - you just need to use one that allows it, e.g.:
String number = "3.350.800";
NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
double value = format.parse(number).doubleValue();
Mix of other answers, no reason to change the , for . and then fetch the German local.
String number = "3,350,800";
NumberFormat format = NumberFormat.getInstance();
double value = format.parse(number).doubleValue();
System.out.println(value);
Output:
3350800.0
you need to use something like this :
String number = "3,350,800";
number = number.replaceAll(",", "");
double value = Double.parseDouble(number);
System.out.println(value);
What number are you trying to get?
3.350.800 is what you're trying to parse as a double,
but that's obviously not a number, since there are "multiple points".
If you just wanna get 3,350,800 as your number, simply change this line -
number = number.replace(",", ".");
to this -
number = number.replace(",", "");
I need to put a constraint for negative values on string variable. For Eg :--
string zeroval = "0.0000"
String x = "";
if (x==null) || (x.equals(zeroval)) { // code which checks if string x has 0 or null value
x = "--" // replace it by --
}
similarly i want to add another piece of code which checks if String x contains any negative values (for eg : "-0.025") and replace it by --
The above String x should not contain null/zero/negative values
Please help
Note :- In order to add negative value check convert the string to float as i cannot use pattern matching technique for eg:- x.equals("-")
Is your input data always meant to contain valid numbers? If so, you could just use:
BigDecimal number = new BigDecimal(text);
if (number.compareTo(BigDecimal.ZERO) <= 0) {
text = "--";
}
This will validate that it really is a number as well as performing the check. Additionally:
It copes with other representations of 0, e.g. "0.00", "0", "+0"
It uses BigDecimal to avoid oddities in binary floating point representations (e.g. a very small positive value being seen as 0). Unlikely to be a problem, but fundamentally you've got decimal data, so you might as well parse it that way.
Convert it to Integer or double using wrapper class.
String number="12.3434"
if(Double.parseDouble(number)<0)
//do stuff here
You could check if the string starts with a "-":
if (x==null) || (x.equals(zeroval) || x.startsWith("-")) {
x = "--";
}
I can't help but feel you are doing something ill-advised by using Strings to represent numerical data.
You could use Double.parseDouble(String) or Float.parseFloat (String). These methods will help you get a double or a float, respectively.
After this, you can easily check if the value is negative.