Luigi

improve mask to real number

7 posts in this topic

#1 ·  Posted

Greetings,

I don't know udf/scripts to mask an data, like this.

I need filtre a data to real number, ex:

1.000.45 = 1000.45 (real to insert in SQLite)

This is script, do you have any better?

 

Global $var = "123.4.55,45"

ConsoleWrite( Mask_Real($var) & @LF)

Func Mask_Real( $var)
    $var = StringRegExpReplace($var, "[^0-9]", ".")
    $var = StringReverse($var)
    Local $point = StringInStr($var, ".")
    $var = StringReplace($var, ".", "")
    $var = StringMid($var, 1, $point-1) & "." & StringMid($var, $point )
    $var = StringReverse($var)
    Return $var
EndFunc

 


Visit my repository

Share this post


Link to post
Share on other sites



#2 ·  Posted

Hi.

How about this:

Global $var = "123.4.55,45"

ConsoleWrite( Mask_Real($var) & @LF)

Func Mask_Real( $var)
    $var = StringReplace($var, ".", "") ; first delete all dots
    $var = StringReplace($var, ",", ".") ; then change the one comma to dot
    Return $var
EndFunc

Regards, Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I made a speedcheck:

Global $var

$iTimer = TimerInit()
For $i = 0 To 1000
    $var = "123.4.55,45"
    Mask_Real($var)
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)

$iTimer = TimerInit()
For $i = 0 To 1000
    $var = "123.4.55,45"
    Mask_Real_Replace($var)
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)

Func Mask_Real( $var)
    $var = StringRegExpReplace($var, "[^0-9]", ".")
    $var = StringReverse($var)
    Local $point = StringInStr($var, ".")
    $var = StringReplace($var, ".", "")
    $var = StringMid($var, 1, $point-1) & "." & StringMid($var, $point )
    $var = StringReverse($var)
    Return $var
EndFunc

Func Mask_Real_Replace( $var)
    $var = StringReplace($var, ".", "")
    $var = StringReplace($var, ",", ".")
    Return $var
EndFunc

Result:

21.7199833301196
5.43823747115426

StringReplace-Mask-Function needs only 1/4 the time. But maybe that isn't relevant in your script.

Conrad

Edited by Simpel
spelling

SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

#4 ·  Posted

@Simpel, thank you for your reply.

I will try.

This is another version, try this:

Func Mask_Real($num = "")
    If $num == "" Then Return ""
    If $num == "." Then Return "0."
    $num = StringRegExpReplace(StringReplace($num, ",", "."), "[^0-9.]", "")
    Local $dot = StringInStr($num, ".")
    If Not $dot Then Return $num
    Return StringReplace(StringMid($num, 1, $dot-1), ".", "") & "." & StringReplace(StringMid($num, $dot+1), ".", "")
EndFunc   ;==>Mask_Real

 


Visit my repository

Share this post


Link to post
Share on other sites

#5 ·  Posted

Are you sure that your result is as you wanted?

Input: 123.4.55,45

Output: 123.45545

I guess output should be: 123455.45

Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

#6 ·  Posted

@Simpel, when I typed the first dot, I see a wrong.

I cannot allow another dot after the first dot, and leave a logic reverse, to take the first dot from right.

For me, the return is good, the doubt is, how improve the performance of the function?

REGEX can be a good option?

Try this:

;~ https://www.autoitscript.com/forum/topic/54909-detecting-when-guictrlinput-change-it-value/

#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <GuiStatusBar.au3>
#include <WinAPI.au3>

Opt('MustDeclareVars', 1)

Global $iInput, $hInput

_Example_Internal()

Func _Example_Internal()
    Local $hGUI
    Local $aPartRightSide[4] = [120, 248, 378, -1]

    $hGUI = GUICreate("(Internal) Edit Get Modify", 400, 300)
    $iInput = GUICtrlCreateInput("123.45", 2, 2, 394, 25)
    $hInput = GUICtrlGetHandle($iInput)

    GUISetState()

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    While Sleep(10) And GUIGetMsg() <> -3
    WEnd
    GUIDelete()
EndFunc   ;==>_Example_Internal

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit
    $hWndFrom = $ilParam
    $iIDFrom = _WinAPI_LoWord($iwParam)
    $iCode = _WinAPI_HiWord($iwParam)
    Switch $hWndFrom
        Case $hInput
            Switch $iCode
                Case $EN_CHANGE
                    GUICtrlSetData($iInput, __Mask_Real(GUICtrlRead($iInput)))
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func __Mask_Real($num = "")
    If $num == "" Then Return ""
    If $num == "." Then Return "0."
    $num = StringRegExpReplace(StringReplace($num, ",", "."), "[^0-9.]", "")
    Local $dot = StringInStr($num, ".")
    If Not $dot Then Return $num
    Return StringReplace(StringMid($num, 1, $dot - 1), ".", "") & "." & StringReplace(StringMid($num, $dot + 1), ".", "")
EndFunc   ;==>__Mask_Real

 


Visit my repository

Share this post


Link to post
Share on other sites

#7 ·  Posted

In my experience StringReplace is much faster then StringRegExpReplace. But sometimes you have to write much more script lines then. But this is only relevant when handling large amounts of input.

Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

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