Converting String into Float

Hi all, I wanna ask you about converting string into float.

I used method parseFloat() to do it and it was worked successfully. The problem is, I get the string using ‘WebUI.getAttribute’ keyword, and maybe causes the value that I try to convert already rounded. Let me explain the example:
- I tired to get value (which is number) from the web using ‘WebUI.getAttribute’, for example 1234567.88
- When I tried to see the value (in string format) using println(), it give me result = 1234567.88
- But when I tried to convert the string value into float, and print the result, it give me result = 1234568.0

this is my script
amountReceive = WebUI.getAttribute(findTestObject(‘float/Page_Main/input_ucReceiveAmt_singletxtIn’), ‘value’, FailureHandling.STOP_ON_FAILURE)

Float amountReceiveFloat = Float.parseFloat(amountReceive)

println(amountReceiveFloat)

I want to ask u how to keep the 2 decimal not rounded (I want the result shown me value = 1234567.88)
Any suggestion from you would be great for me. Thanks :slight_smile:

Hi Willis,

are you sure that you get correct float in first step? Because following code works as expected:

String amountReceive = "123456.78"Float amountReceiveFloat = Float.parseFloat(amountReceive)println(amountReceiveFloat)println(amountReceiveFloat.class)// console123456.78class java.lang.Float

Hi Marek, thanks for your response :slight_smile:

First of all, I think the example that you gave me is little bit different with my case. I must tell that the string value was from ‘WebUI.getAttribute()’ function, not manually declare like your example.
The case is:
- the value that I got with ‘WebUI.getAttribute’ function is from numeric field (default 0.00), not from free text
- when I used the getAttribute method, it gave me return type as string (I have no idea is it possible to get it on float type?)
- so in my case, I need to parse the string type into float, and what happened is the decimal value just rounded automatically (like the example before, the string that I got = 1234567.88, but when it parsed the result = 1234568.0 )

Do you have another solution related to my case? How can I keep the decimal not rounded?

*you can see my code below

float.png