Use RegEx to round/fixed minimum number of decimals...

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Use RegEx to round/fixed minimum number of decimals...

Post by Grovkillen » Thu Sep 26, 2013 12:52 pm

Hello,

Here's a code snippet I just made which will cut a given number to a set number of decimals. It might give someone else an idea...

Code: Select all

//Change this to what you are using as a separator
Let>DecimalSeparator=.
//Change this to the number of decimals you want
Let>NumbersOfDecimals=2
//Mess around with this...
Let>ValueToRound=121210125.490521010210

Let>NumbersOfDecimals_Temp=%NumbersOfDecimals%+1
Let>RegExPattern=(\d+\%DecimalSeparator%)\d{0,%NumbersOfDecimals_Temp%}
RegEx>%RegExPattern%,%ValueToRound%,0,MatchArray,NumMatches,,,
If>NumMatches>0
    Let>RegExPattern=%DecimalSeparator%([0-9]){0,%NumbersOfDecimals%}(?=[5-9])
    RegEx>%RegExPattern%,%MatchArray_1%,0,,NumMatches,,,
    If>NumMatches>0
        Let>RegExPattern=(\d+\%DecimalSeparator%)\d{0,%NumbersOfDecimals%}
        RegEx>%RegExPattern%,%MatchArray_1%,0,MatchArray,NumMatches,,,
        RegEx>\%DecimalSeparator%,%MatchArray_1%,0,,NumMatches,1,.,MatchArray_1
        Let>AddRound={1/(10^%NumbersOfDecimals%)}
        Let>MatchArray_1=%MatchArray_1%+%AddRound%
        RegEx>\.,%MatchArray_1%,0,,NumMatches,1,%DecimalSeparator%,MatchArray_1
    Else>
        Let>RegExPattern=(\d+\%DecimalSeparator%)\d{0,%NumbersOfDecimals%}
        RegEx>%RegExPattern%,%MatchArray_1%,0,MatchArray,NumMatches,,,
        RegEx>\.,%MatchArray_1%,0,,NumMatches,1,%DecimalSeparator%,MatchArray_1
    Endif>
    MDL>MatchArray_1
Else>
    MDL>No " %DecimalSeparator% " found!%CRLF%%CRLF%%ValueToRound%
Endif>
Let>ME=%Script%

Running: 15.0.24
version history

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Thu Sep 26, 2013 2:53 pm

This is code I have used through the years.
One big advantage of this approach, is that it is easier for someone else to understand. Yes, RegEx is extremely powerful, but it isn't the easiest to read and know what the code is doing.

Code: Select all

//Change this to the number of decimal places you want
Let>NumDecimals=2
//Mess around with this...
Let>ValueToRound=123.45678

// Set Multiplier as 10, 100, 1000 etc.
Let>Multiplier={Power(10,%NumDecimals%)}

Let>NewValue={Round(%ValueToRound%*%Multiplier%)}
Let>NewValue=NewValue/Multiplier

MDL>       Initial Value: %ValueToRound%%CRLF%Rounded Value: %NewValue%
Thanks,
Jerry

[email protected]

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Thu Sep 26, 2013 3:05 pm

This is code I have used through the years.
One big advantage of this approach, is that it is easier for someone else to understand. Yes, RegEx is extremely powerful, but it isn't the easiest to read and know what the code is doing.

Code: Select all

//Change this to the number of decimal places you want
Let>NumDecimals=2
//Mess around with this...
Let>ValueToRound=123.45678

// Set Multiplier as 10, 100, 1000 etc.
Let>Multiplier={Power(10,%NumDecimals%)}

Let>NewValue={Round(%ValueToRound%*%Multiplier%)}
Let>NewValue=NewValue/Multiplier

MDL>       Initial Value: %ValueToRound%%CRLF%Rounded Value: %NewValue%
Thanks,
Jerry

[email protected]

User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Thu Sep 26, 2013 4:04 pm

Thanks for sharing Jerry, my problem was that the values all used "," for decimal separator and the {} expressions aren't compatable with that... it only uses "dot".
Let>ME=%Script%

Running: 15.0.24
version history

thomasusa80
Newbie
Posts: 1
Joined: Wed Oct 01, 2014 4:19 am

Re: Use RegEx to round/fixed minimum number of decimals...

Post by thomasusa80 » Wed Oct 01, 2014 4:34 am

It is program code I have utilized through the years.
A single large benefit from this process, can be it's less complicated with regard to other people to comprehend. Indeed, RegEx is quite powerful, however it just isn't the simplest to read and understand what this program code is performing.
We are the pioneers in providing 300-206 dumps and ASQ tutorials with 100% exam pass guarantee. Download our latest Stanford University .

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts