Jump to content

String to float


Recommended Posts

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
Link to comment
Share on other sites

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 ...
 

Link to comment
Share on other sites

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 ...
 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

...

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

Link to comment
Share on other sites

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 ...
 

Link to comment
Share on other sites

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

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

 

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...