round function

Help with writing and playing macros

round function

Postby reinim19 » Thu Sep 20, 2007 12:31 pm

Hello, I'm having numbers and want them to be cut (and round) in the following way

1 > 1 (or 1.0)
1.2 > 1.2
1.23 > 1.2
1.25 > 1.3
1.267 > 1.3

as you see, the numbers can have a different amount of post-comma digits

how to realize that with a Macro?
User avatar
reinim19
Newbie
 
Posts: 4
Joined: Mon Aug 06, 2007 11:00 pm

Re: round function

Postby Mofi » Thu Sep 20, 2007 1:09 pm

Nearly impossible with a macro. Rounding down is not a problem. Rounding up is horrible for a number like 9999999.98 > 10000000.0

I suggest to do that with 2 macros. The first one finds the numbers with at least 2 digits and round it down which is pretty easy. The second one is called by the first one when a round up is required.

Macro "Find Numbers"

InsertMode
ColumnModeOff
HexOff
UnixReOff
Find RegExp "[0-9]+.[0-9][0-9]"
IfFound
Key LEFT ARROW
IfCharIs "01234"
Find RegExp "[0-9]+"
Replace ""
Else
Find RegExp "[0-9]+"
Replace ""
PlayMacro 1 "RoundUp"
EndIf
EndIf

The second macro is a variant of my universal "CountUp" macro posted at counter ? It ignores the decimal point when moving the cursor left and evaluating the character.

Macro "RoundUp"

InsertMode
"|"
Key LEFT ARROW
Key LEFT ARROW
OverStrikeMode
Loop
IfCharIs "0"
"1"
ExitLoop
EndIf
IfCharIs "1"
"2"
ExitLoop
EndIf
IfCharIs "2"
"3"
ExitLoop
EndIf
IfCharIs "3"
"4"
ExitLoop
EndIf
IfCharIs "4"
"5"
ExitLoop
EndIf
IfCharIs "5"
"6"
ExitLoop
EndIf
IfCharIs "6"
"7"
ExitLoop
EndIf
IfCharIs "7"
"8"
ExitLoop
EndIf
IfCharIs "8"
"9"
ExitLoop
EndIf
IfCharIs "9"
"0"
Key LEFT ARROW
IfColNum 1
InsertMode
"1"
ExitLoop
EndIf
Key LEFT ARROW
IfCharIs "."
Key LEFT ARROW
EndIf
IfCharIs "0123456789"
Else
Key RIGHT ARROW
InsertMode
"1"
ExitLoop
EndIf
EndIf
EndLoop
InsertMode
Find "|"
Replace ""

The macro property Continue if a Find with Replace not found or Continue if search string not found should be checked for both macros.

Add UnixReOn or PerlReOn (v12+ of UE) at the end of the first macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.

Important: You have to create first the macro "RoundUp"! The macro name is case-sensitive in the PlayMacro command and the macro must already exist!
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros