Jump to content

Convert String to Float


Recommended Posts

Lets say I do use the Number function provided. It has one hitch though. If my String is "12.0", the output of the Number function is the integer 12 and not the float number 12.0

Round does not work either. It automatically strips the 0. I want an 'exact' representation of the string in an integer form. Would love any help on this. Feel free to reprimand me if i am doing something incredibly stupid.

Thanks

Link to comment
Share on other sites

in an integer form

That means that there is no trailing zero... Why don't you try useing $var (or) 12 & '.0'?

Edited by Wolvereness

Offering any help to anyone (to my capabilities of course)Want to say thanks? Click here! [quote name='Albert Einstein']Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.[/quote][quote name='Wolvereness' date='7:35PM Central, Jan 11, 2005']I'm NEVER wrong, I call it something else[/quote]

Link to comment
Share on other sites

  • Developers

Perhaps you use StringFormat() when you present the variable... is StringFormat just beta? I forget...

<{POST_SNAPBACK}>

Nah... in since 102

$a =12
msgbox(0,'',StringFormat("%.1f",$a))

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks for your responses guys. Let me explain a little more clearly what I am trying to do here.

I have a list of builds in a folder. Each build has a name of the form "xxxx.x" where each of the 'x' is an integer. Now I go through all the build names and see which one is the latest by comparing their values. When I get the name of the build, it is obviously returned as a string.

I call the Number / Round function on this string to get the float value of the string so that I can compare two builds and see which one is the latest. If I leave them as a string, build 5006.2 will be considered smaller (and hence built before) build 53.0. Thus I have to have them in the float form before I compare them.

I need to use this build number to get an FTP download of the build. The problem comes when the build number is something like '5007.0' My program finds that this is the latest build but rounds it to 5007 when I use either the Number or Round or StringFormat function. The FTP client then quits claiming that there is no such build on the server as it looks for 5007 when it should be searching for 5007.0

Hence I was wondering if there is a function or utility that I am missing that will take care of this. I used Number($build, 3) but it didnt work and neither did StringFormat. Please let me know if there is a way out for me.

Thanks

Link to comment
Share on other sites

...

Hence I was wondering if there is a function or utility that I am missing that will take care of this. I used Number($build, 3) but it didnt work and neither did StringFormat. Please let me know if there is a way out for me.

Thanks

<{POST_SNAPBACK}>

I don't think there is a function. How about reformatting the number after performing the comparison. The following example also allows multiple zeros as the decimal.

$n1 = "63.1"
; $n1 = "6300.1"; for testing
$n2 = "5006.000"
$Dec1 = StringLen($n1) - Stringlen(Int($n1)) - 1
$Dec2 = StringLen($n2) - Stringlen(Int($n2)) - 1
$n1 = round($n1, 1)
$n2 = round($n2, 1)

If $n2 > $n1 Then
   MsgBox(4096, "1", $n2 & " is > " & $n1)
 ; reformat the number for downloading.
   $n3 = StringFormat("%." & $Dec2 & "f",$n2)
Else
   MsgBox(4096, "1", $n1 & " is > " & $n2)
 ; reformat the number for downloading.
   $n3 = StringFormat("%." & $Dec1 & "f",$n1)
EndIf

MsgBox(4096, "", "Download " & $n3)

exit

Phillip

Link to comment
Share on other sites

Thanks a lot Phillip.

Your solution works like a charm and thats what I am doing currently as a workaround. So I guess I was right in thinking that we can really not convert it to float and have trailing 0s after the decimal. Your solution ends with returning a string with trailing 0s.

Now I am returning an array containing a the float and the actual representation of it in a string to be able to use the float value to compare but use the string value to recognise the folder. Thanks to all for their input

Link to comment
Share on other sites

Your solution works ...

I'm happy to hear it helped. I just revisited the code I sent, and I wonder why I went through the trouble of converting back and forth. In case you would like a slightly easier to follow coding (and probably a tiny bit faster), check out the following. On the other hand, if it's working why mess with it?

$n1 = "63.1"
; $n1 = "6300.1"; for testing
$n2 = "5006.000"
$n1b = round($n1, 1)
$n2b = round($n2, 1)

If $n2b > $n1b Then
   MsgBox(4096, "1", $n2b & " is > " & $n1b)
; reformat the number for downloading.
   $n3 = $n2
Else
   MsgBox(4096, "1", $n1b & " is > " & $n2b)
; reformat the number for downloading.
   $n3 = $n1
EndIf
MsgBox(4096, "", "Download " & $n3)
Exit

Phillip

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...