Jump to content

Indian Style Digit grouping using RegEx


Recommended Posts

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

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

 

Link to comment
Share on other sites

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)

 

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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

 

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