siva1612 Posted March 21, 2017 Posted March 21, 2017 (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 March 21, 2017 by siva1612
ViciousXUSMC Posted March 21, 2017 Posted March 21, 2017 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
mikell Posted March 21, 2017 Posted March 21, 2017 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
ViciousXUSMC Posted March 21, 2017 Posted March 21, 2017 Something tells me that IRL you actually have found a way to make RegEx a spoken language.
iamtheky Posted March 21, 2017 Posted March 21, 2017 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) Reveal hidden contents ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted March 21, 2017 Posted March 21, 2017 @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 ? iamtheky 1
iamtheky Posted March 21, 2017 Posted March 21, 2017 @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. mikell 1 Reveal hidden contents ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted March 21, 2017 Posted March 21, 2017 Well. I suddenly feel better... Too violent changes are not good for my heart
siva1612 Posted March 23, 2017 Author Posted March 23, 2017 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now