Sign in to follow this  
Followers 0
siva1612

Indian Style Digit grouping using RegEx

9 posts in this topic

#1 ·  Posted (edited)

I found the below code for digit grouping numbers. This groups numbers in millions and hundreds convention (1000000 to 1,000,000). Can someone help me with getting a similar functions for grouping in Indian style lakhs and crores convention (1000000 to 10,00,000)?

Also the below code was written for a edit control which might accept both text and numbers. But for my case it is a single line input control and it accepts only numbers.

I'm trying to understand this RegEx stuff and haven't succeeded yet!

Func AddComma($sDigit)
    SetExtended(1)
    While @extended
        $sDigit = StringRegExpReplace($sDigit, '(?m)(^|[^.\d])(\d+)(\d{3})', '\1\2,\3')
    WEnd
    Return $sDigit
EndFunc   ;==>AddComma

;For Removing the Added Commas
Func RemoveComma($sString)
    SetExtended(1)
    While @extended
        $sString = StringRegExpReplace($sString, '(\d+)(,)(\d{3})', '\1\3')
    WEnd
    Return $sString
EndFunc   ;==>RemoveComma

 

Edited by siva1612

Share this post


Link to post
Share on other sites



#2 ·  Posted

Might be able to use _StringInsert() if you know your data is always going to be good data.

#Include <String.au3>

MsgBox(0, "", InsertComma(1000000))
MsgBox(0, "", InsertComma(56662349))
MsgBox(0, "", InsertComma(60002343233))

Func InsertComma($iNum)
    $iChar = StringLen($iNum)
    If $iChar > 3 Then
        $i = -3
        Do
        $iNum = _StringInsert($iNum, ",", $i)
        $i -= 4
        Until $i < ($iChar*-1)
    EndIf
    Return $iNum
EndFunc

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

This one was a little tricky  :)

Msgbox(0,"", AddComma("30000000000.1234567") )

Func AddComma($sDigit)
       Return StringRegExpReplace($sDigit, '\G(\d+?)(?=(\d{2})*\d{3}(\D|$))', '$1,')
EndFunc   ;==>AddComma

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

Something tells me that IRL you actually have found a way to make RegEx a spoken language. :)

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

I made a thing, if I could clean up the extra comma cleanly it wouldn't be such a ridiculous rube Goldberg machine.

$sNum = "23456789"     ; "12,34,56,789.01"

$a = stringleft(  StringReverse(StringRegExpReplace(stringtrimleft(stringreverse($sNum) , 3) , "(\d\d)" , "$1,")) & "," & stringright($sNum , 3) , 1) = "," ? stringtrimleft(StringReverse(StringRegExpReplace(stringtrimleft(stringreverse($sNum) , 3) , "(\d\d)" , "$1,")) & "," & stringright($sNum , 3) , 1) : StringReverse(StringRegExpReplace(stringtrimleft(stringreverse($sNum) , 3) , "(\d\d)" , "$1,")) & "," & stringright($sNum , 3)

msgbox(0, '' , $a)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#6 ·  Posted

@ViciousXUSMC
How did you guess ?  :)
BTW the main idea here was to use \G to cause a 'forced failure' when the regex engine reaches the decimal dot
This makes the expression able to work on any number with or without decimals

@iamtheky
Am I dreaming or you really did use regex ?  :blink:

1 person likes this

Share this post


Link to post
Share on other sites

#7 ·  Posted

@mikell

only until I figure out how to do it with stringmid and strenglen math, I have to do real work but in a few hours I shall undo that blasphemy.

1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#8 ·  Posted

Well. I suddenly feel better... Too violent changes are not good for my heart  :)

Share this post


Link to post
Share on other sites

#9 ·  Posted

I did it using this

Func AddComma($sDigit)
    $sDigit = StringRegExpReplace($sDigit, '(\d+)(\d{3})', '\1,\2')
    While @extended
        $sDigit = StringRegExpReplace($sDigit, '(^|[^,\d])(\d+)(\d{2})', '\1\2,\3')
    WEnd
    Return $sDigit
EndFunc   ;==>AddComma

Func RemoveComma($sString)
    SetExtended(1)
    While @extended
        $sString = StringRegExpReplace($sString, '(\d+)(,)(\d+)', '\1\3')
    WEnd
    Return $sString
EndFunc   ;==>RemoveComma

 

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  
Followers 0