Jump to content

separator thousands numbers


Maurizio
 Share

Recommended Posts

on $Input I write 2400 I would like to write 2.400 on $Input2. I searched but found only very complex solutions. Could it be so problematic? Thank you

 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $Input1
       GUICtrlSetData($Input2, GUICtrlRead($Input1) + GUICtrlRead($Input2))
    EndSwitch
WEnd

 

Link to comment
Share on other sites

  • Moderators

In the future, it would help immensely if you would post runnable code so we don't have to re-write from scratch. Try something like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $hGUI = GuiCreate("Test", 300, 400)
GUISetState(@SW_SHOW)
$Input1 = GUICtrlCreateInput("", 10, 10, 100, 40)
$Input2 = GUICtrlCreateInput("", 10, 60, 100, 40)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $DisplayNumber = StringRegExpReplace((GUICtrlRead($Input1) + GUICtrlRead($Input2)), "(?!\.)(\d)(?=(?:\d{3})+(?!\d))(?<!\.\d{1}|\.\d{2}|\.\d{3}|\.\d{4}|\.\d{5}|\.\d{6}|\.\d{7}|\.\d{8}|\.\d{9})", "\1,")
            GUICtrlSetData($Input2, $DisplayNumber)
    EndSwitch
WEnd

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

You could use , instead of . as a separator.

From @mikell : what-replaced-_stringaddthousandssep

Local $sInput, $sOutput

$sInput  = "2400"
$sOutput = StringRegExpReplace($sInput, '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
MsgBox(0, "Result before and after", $sInput & @CRLF & $sOutput)

$sInput  = "1002400.22"
$sOutput = StringRegExpReplace($sInput, '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
MsgBox(0, "Result before and after", $sInput & @CRLF & $sOutput)

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Moderators

I misread your OP, and wrote it to put in a comma rather than a period, but Musashi's is cleaner nonetheless.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

@Maurizio

First of all, some basics regarding period and comma as separators that might interest you :

Spoiler

http://www.languageediting.com/format-numbers-eu-vs-us/

Most European countries (as well many other countries in the world):
300.000 or 300 000 means three hundred thousand
€120.000,99 or €120 000,99 means one hundred twenty thousand euros and ninety-nine cents

The UK (plus other countries in the world, like the US, Australia, and New Zealand):
300,000 means three hundred thousand
€120,000.99 means one hundred twenty thousand euros and ninety-nine cents

SI System:
300 000 means three hundred thousand
€120 000,99 or €120 000.99 means one hundred twenty thousand euros and ninety-nine cents

There was even a standard UDF Function called _StringAddThousandsSep in old AutoIt versions, but it has been removed.

Here is an example with the removed function where you can set the separators :

#include <String.au3>

Local $nAmount, $sDelimted
$nAmount   = 2400
$sDelimted = _StringAddThousandsSep($nAmount)
MsgBox(64, 'Info', $sDelimted)

$nAmount   = 12040.43
$sDelimted = _StringAddThousandsSep($nAmount)
MsgBox(64, 'Info', $sDelimted)

; #FUNCTION# ====================================================================================================================
; Name...........: _StringAddThousandsSep
; Description ...: Returns the original numbered string with the Thousands delimiter inserted.
; Syntax.........: _StringAddThousandsSep($sString[, $sThousands = -1[, $sDecimal = -1]])
; Parameters ....: $sString - The string to be converted.
; $sThousands - Optional: The Thousands delimiter
; $sDecimal   - Optional: The decimal delimiter
; Return values .: Success - The string with Thousands delimiter added.
; ==============================================================================================================================
Func _StringAddThousandsSep($sString, $sThousands = ".", $sDecimal = ",")
    Local $aNumber, $sLeft, $sResult = "", $iNegSign = "", $DolSgn = ""
    If Number(StringRegExpReplace($sString, "[^0-9\-.+]", "\1")) < 0 Then $iNegSign = "-" ; Allows for a negative value
    If StringRegExp($sString, "\$") And StringRegExpReplace($sString, "[^0-9]", "\1") <> "" Then $DolSgn = "$" ; Allow for Dollar sign
    $aNumber = StringRegExp($sString, "(\d+)\D?(\d*)", 1)
    If UBound($aNumber) = 2 Then
        $sLeft = $aNumber[0]
        While StringLen($sLeft)
            $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
            $sLeft = StringTrimRight($sLeft, 3)
        WEnd
        $sResult = StringTrimLeft($sResult, 1); Strip leading thousands separator
        If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1] ; Add decimal
    EndIf
    Return $iNegSign & $DolSgn & $sResult ; Adds minus or "" (nothing)and Adds $ or ""
EndFunc   ;==>_StringAddThousandsSep

 

25 minutes ago, Maurizio said:

and how do I insert his?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $hGUI = GuiCreate("Test", 300, 400)
GUISetState(@SW_SHOW)
$Input1 = GUICtrlCreateInput("", 10, 10, 100, 40)
$Input2 = GUICtrlCreateInput("", 10, 60, 100, 40)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $DisplayNumber = StringRegExpReplace(GUICtrlRead($Input1), '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
            GUICtrlSetData($Input2, $DisplayNumber)
    EndSwitch
WEnd

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

It works but I realized that when I add another digit to the existing one, it doesn't consider the 3 zeros.
If I put on $ Input1 10000, in the box $ Input2 it writes 10,000.
If I rewrite 10000 in the $ Input1 box, I will not have 20,000 in the $ Input2 box, but strangely 10,010

Link to comment
Share on other sites

1 hour ago, Maurizio said:

If I put on $ Input1 10000, in the box $ Input2 it writes 10,000.

I can confirm that.

 

1 hour ago, Maurizio said:

If I rewrite 10000 in the $ Input1 box, I will not have 20,000 in the $ Input2 box, but strangely 10,010

?

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Developers
2 hours ago, Maurizio said:

If I rewrite 10000 in the $ Input1 box, I will not have 20,000 in the $ Input2 box, but strangely 10,010

Are you adding the current value of $input2 (10,000) together with 10000 in stead of the original 10000 value?

Jos

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

 

If in the Delivery box I write 10000 in the total boxes it gives me 10,000 in the right way.
if I rewrite 10000 again and click on the button, in the totals I will not have 20,000 but 10,010.
I can't explain why, you certainly do. :)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\maurizio\desktop\autoit\koda\gestione spedizioni.kxf
$Form1_1 = GUICreate("Resoconto", 908, 527, 192, 124)
$Group1 = GUICtrlCreateGroup("Spedizioni", 32, 40, 441, 465)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$Cargo = GUICtrlCreateLabel("Cargo", 56, 96, 108, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Input1=GUICtrlCreateInput("", 192, 96, 81, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input2=GUICtrlCreateInput("", 336, 96, 105, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
GUICtrlCreateLabel("Consegnati", 200, 56, 62, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Tot = GUICtrlCreateLabel("Totale", 352, 56, 80, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Label22 = GUICtrlCreateLabel("Totale", 512, 464, 62, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Input29=GUICtrlCreateInput("", 704, 344, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input30=GUICtrlCreateInput("", 704, 384, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input31 = GUICtrlCreateInput("", 704, 425, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input32 = GUICtrlCreateInput("", 704, 464, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetColor(-1, 0x008000)
$Button3 = GUICtrlCreateButton("E", 288, 128, 33, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

Case $button3
         Case $Input1
       GUICtrlSetData($Input2, GUICtrlRead($Input1) + GUICtrlRead($Input2))
       $DisplayNumber = StringRegExpReplace((GUICtrlRead($Input1) * (2000) + GUICtrlRead($Input29)), '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
       GUICtrlSetData($Input29,$DisplayNumber)
       $DisplayNumber = StringRegExpReplace((GUICtrlRead($Input1) * (2000) + GUICtrlRead($Input30)), '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
       GUICtrlSetData($Input30,$DisplayNumber)
       $DisplayNumber = StringRegExpReplace((GUICtrlRead($Input1) * (4000) + GUICtrlRead($Input32)), '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
       GUICtrlSetData($Input32,$DisplayNumber)

    EndSwitch
WEnd

 

Link to comment
Share on other sites

  • Developers

Let's make this a little simpler. This script demonstrates your problem and the possible solution:

$a = 12345
$b = StringRegExpReplace($a, '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
$b2wrong = $a + $b
$b2correct = $a + StringReplace($b, ',', '')
$b2new = StringRegExpReplace($b2correct, '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
ConsoleWrite('$a = ' & $a & @CRLF)
ConsoleWrite('$b = ' & $b & @CRLF)
ConsoleWrite('$b2wrong = ' & $b2wrong & @CRLF)
ConsoleWrite('$b2correct = ' & $b2correct & @CRLF)
ConsoleWrite('$b2new = ' & $b2new & @CRLF)

output:

$a = 12345
$b = 12,345
$b2wrong = 12357
$b2correct = 24690
$b2new = 24,690

Jos

Edited by Jos

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

  • Developers

Come on ...  have a look, understand and give it a try yourself so you do really understand what it does. ;)  

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

Yes, you are right, things need to be understood, I have been following you for a while, and searching I found what I needed, and for a while I have avoided copy and paste, and I write to learn. Not being a programmer but a geek, I get there in things, and this allowed me to make small working programs. But I realize that without the basics the more complex things are difficult if I don't have working examples, and frankly what you wrote, to my knowledge, they look like hieroglyphics to me :)

Link to comment
Share on other sites

  • Developers

I understand that the rexgex stuff is overwhelming and pretty complex, but you already had that in your code, so just ignore that. 
When you then look at the other statement I made to get the correct new value it is really only removing the comma again before adding the new value to it. ;) 

Jos  

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

  • Developers
1 hour ago, Maurizio said:

I can't figure out what I need to change

Try baby steps and show what you have done.  Try breaking it up in steps like I have done in stead of:

GUICtrlSetData($Input2, GUICtrlRead($Input1) + GUICtrlRead($Input2))

Read those values into a variable ( like I have done in my simple example and apply the same logic as I have shown. 
So now with your next post you will have to be able to show us something because "doesn't work" or "I don't know" is not allowed anymore in this thread. ;) 

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

 

my unpublished attempts are limited like this example I have published now. But I know I'm wrong, but I can't figure out what to use from what you posted above, and so I'm stuck. I appreciate your help and stimulus, but my commitment is there because I want to learn, but on this part of the code, it is like asking an auto electrician to change a heart valve. :)

 

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\maurizio\desktop\autoit\koda\gestione spedizioni.kxf
$Form1_1 = GUICreate("Resoconto", 908, 527, 192, 124)
$Group1 = GUICtrlCreateGroup("Spedizioni", 32, 40, 441, 465)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$Cargo = GUICtrlCreateLabel("Cargo", 56, 96, 108, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Input1=GUICtrlCreateInput("", 192, 96, 81, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input2=GUICtrlCreateInput("", 336, 96, 105, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
GUICtrlCreateLabel("Consegnati", 200, 56, 62, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Tot = GUICtrlCreateLabel("Totale", 352, 56, 80, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Label22 = GUICtrlCreateLabel("Totale", 512, 464, 62, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Input29=GUICtrlCreateInput("", 704, 344, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input30=GUICtrlCreateInput("", 704, 384, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input31 = GUICtrlCreateInput("", 704, 425, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
$Input32 = GUICtrlCreateInput("", 704, 464, 169, 28, BitOR($GUI_SS_DEFAULT_INPUT,$ES_RIGHT,$ES_NUMBER))
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetColor(-1, 0x008000)
$Button3 = GUICtrlCreateButton("E", 288, 128, 33, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
$a = 12345
$b = StringRegExpReplace($a, '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
$b2wrong = $a + $b
$b2correct = $a + StringReplace($b, ',', '')
$b2new = StringRegExpReplace($b2correct, '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
ConsoleWrite('$a = ' & $a & @CRLF)
ConsoleWrite('$b = ' & $b & @CRLF)
ConsoleWrite('$b2wrong = ' & $b2wrong & @CRLF)
ConsoleWrite('$b2correct = ' & $b2correct & @CRLF)
ConsoleWrite('$b2new = ' & $b2new & @CRLF)

$a = 12345
$b = 12,345
$b2wrong = 12357
$b2correct = 24690
$b2new = 24,690

Case $button3
         Case $Input1
       GUICtrlSetData($Input2, GUICtrlRead($Input1) + GUICtrlRead($Input2))
       $DisplayNumber = ConsoleWrite('$b2new = ' & $b2new & @CRLF)((GUICtrlRead($Input1) * (2000) + GUICtrlRead($Input29)),
       GUICtrlSetData($Input29,$DisplayNumber)
       $DisplayNumber = StringRegExpReplace((GUICtrlRead($Input1) * (2000) + GUICtrlRead($Input30)), '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
       GUICtrlSetData($Input30,$DisplayNumber)
       $DisplayNumber = StringRegExpReplace((GUICtrlRead($Input1) * (4000) + GUICtrlRead($Input32)), '\G(\d+?)(?=(\d{3})+(?:\D|$))', '$1,')
       GUICtrlSetData($Input32,$DisplayNumber)

    EndSwitch
WEnd

 

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