Jump to content
Sign in to follow this  
Klovis

String to float

Recommended Posts

Klovis

I have this data coming to me in a tcp connection and i need it to be a simple number with no letters or 'e's so how would I make this string into a float

9.426686e+010

the data is a form of scientific notation

Edited by Klovis

Share this post


Link to post
Share on other sites
sigil

You can use _WinAPI_FloatToInt().

Edited by sigil

Share this post


Link to post
Share on other sites
MvGulik

You can use _WinAPI_FloatToInt().

:)

Its how to make a number in "string" form into a real Number() value.

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
Klovis

So, the winApi thing wont work?

Share this post


Link to post
Share on other sites
MvGulik

So, the winApi thing wont work?

Depends on what your looking for. As you mentioned Float. And that winapi function is returning a Int. I don't think it will work for what you have in mind.

---

Think I better quote myself with a little adjustment.

... real Number() value.

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
sigil

Klovis, can you give an example of a string you would get from the tcp connection and what you would want it to return?

e.g.,

input: "3.552e+05"

output: 355200

Edited by sigil

Share this post


Link to post
Share on other sites
ProgAndy

...

MsgBox(0, '', Number("9.426686e+010"))
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
MvGulik

Well, if where going to post code ...

#include <winapi.au3>
main()
Func main()
    Local $str = '9.426686e+010'
    DebugOut('$str', $str) ;### Debug DebugOut.
    ;; $str = "9.426686e+010"

    Local $num = Number($str)
    DebugOut('$num', $num) ;### Debug DebugOut.
    ;; $num = 94266860000 {Double}

    Local $wapi = _WinAPI_FloatToInt($str)
    DebugOut('$wapi', $wapi) ;### Debug DebugOut.
    ;; $wapi = 2139095040 {Int64}

EndFunc

Func DebugOut($sMsg='',$vData='!NuLL!',$iErr=@error,$iExt=@extended,$iLine=@ScriptLineNumber)
If Not(String($vData)=='!NuLL!')Then
If IsString($vData)Then
If $vData Then
$sMsg&=' = '&'"'&$vData&'"' ;; StringReplace($vData,'"','""',0,1)
Else
$sMsg&=' = '&'{EmptyString}'
EndIf
Else
$sMsg&=' = '&String($vData)&' {'&VarGetType($vData)&'}'
EndIf
EndIf
If $iErr Or $iExt Then $sMsg='['&$iErr&','&$iExt&']('&$iLine&',) '&$sMsg
Return SetError($iErr,$iExt,ConsoleWrite($sMsg&@CRLF))
EndFunc
:)

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
Klovis

Thanks guys got the answer!!!

Share this post


Link to post
Share on other sites
wolf9228

I have this data coming to me in a tcp connection and i need it to be a simple number with no letters or 'e's so how would I make this string into a float

9.426686e+010

the data is a form of scientific notation

int sscanf ( const char * str, const char * format, ...);

Read formatted data from string

http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

http://msdn.microsoft.com/en-us/library/zkx076cy.aspx

$RT = sscanf("9.426686e+010","%f","float")
if Not @error Then MsgBox(0,"",$RT)

Func sscanf($Str,$Singleformat,$OutDataType)
$RT = DllCall("msvcrt.dll","int:cdecl","sscanf","str",$Str,"str",$Singleformat,$OutDataType & "*",0)
if @error Then Return SetError(1,0,0)
Return SetError($RT[0] == 0,0,$RT[3])
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.