Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Conversion of String directly to float

1. ## Conversion of String directly to float

Hi All,

Is there a way I can convert a String to float without converting it to an Integer first.

Scenario is:- Expected Result since it is coming from excel is:- 0.35000000, Actual Result which is displayed in application is 0.35 if i convert it to Integer it gives me 0.

Kindly Suggest.

Thanks and Regards,

Nupur

2. ## Re: Conversion of String directly to float

I have implemented this function to remove the trailing zeros from a string holdng a decimal representation.

Possible inputs:
print(RemZero("0.350000")) //returns "0.35"
print(RemZero("11.110100")) // returns "11.1101"
print(RemZero("12.0")) // returns "12"

<font class="small">Code:</font><hr /><pre>
[-] String RemZero(String s)
[ ] Integer j=0, i = 0
[ ] i=Len(s)
[-] for(j=i;j&gt;=0;j--)
[-] if(s[j]!="0" &amp;&amp; s[j]!=".")
[ ] return(SubStr(s,1,j))
[ ] break
[-] else if(s[j]==".")
[ ] return(SubStr(s,1,(j-1)))
[ ] break
</pre><hr />

Hope this helps ...

Can you try this plz,
Populate the data in your excel by appending ' before your numeric values for e.g '0.35

Also try changing the format of that column in your excel file to "TEXT".

I think once you address the following and try reading the same thru ST you will get the value as 0.35 and not 0.350000

2lz

3. ## Re: Conversion of String directly to float

Hi you can use this
[ QUOTE ]

[-] REAL StringToFloat(STRING sNumber)
[ ] return([REAL]val(sNumber))

[/ QUOTE ]

paste this function in ur frame file and you can call it as follows.
[ QUOTE ]

StringToFloat("0.35")

[/ QUOTE ]

Result:
0.350000

4. ## Re: Conversion of String directly to float

Hi Krishna,

I guess what Nupur is trying to achieve here by converting a STRING to FLOAT is to get rid of those trailing zeros coz the actual value(0.35) from her application omits those zeros after the decimal point &amp; reading from the excel doesn't (0.35000) &amp; so as converting it to FLOAT in your case.

Nupur, kindly let me know if I have misinterpreted your post.

2lz

5. ## Re: Conversion of String directly to float

Hi Krishna, 2Lz

Thanks for the response. I have incorporated as you suggested and my script is working perfect.

Thanks a ton for the help. I always learn something good from both of you. Kudos to you...

Thanks and Regards,
Nupur [img]/images/graemlins/smile.gif[/img]

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.