I have inherited a method in postscript that takes a decimal number and returns a 2 places decimal number, will round up as necessary.
The method has an error in that it will round up the value after the decimal place but does not up the integer part of the numeric number. For example, if the number was 4.895, the method will correctly return 4.90, but if the number is 4.995 the method will wrong return 4.00, and I cannot work out how to amend the method.
Below is the method in question.
formatNumberString is the main method (anything with % is a comment) this has another method tmpNum (converts string into a number) also has another method intNum (gets the integer part of the number) also has another method decNum (gets the decimal part of the number)
/formatNumberString { % stack: number as a string
/tmpNum exch def % 1234567.8901
/intNum tmpNum cvr floor cvi def % 1234567 (int)
/decNum tmpNum cvr intNum sub 100 mul round cvi def % 0.8901 changing to 89 (int)
/units intNum 1000 mod def
/remaining intNum units sub 1000 div cvi def
remaining 0 eq {
/formatNumStr units numToStr def
} {
/formatNumStr units threeDigitStr def
} ifelse
{
remaining 0 eq {
exit
} if
/units remaining 1000 mod def
/remaining remaining units sub 1000 div cvi def
remaining 0 eq {
/formatNumStr units numToStr (,) mergeStrings formatNumStr mergeStrings def
} {
/formatNumStr units threeDigitStr (,) mergeStrings formatNumStr mergeStrings def
} ifelse
} loop
%formatNumStr (.) mergeStrings decNum threeDigitStr mergeStrings
formatNumStr (.) mergeStrings decNum 2 digitStr mergeStrings
} bind def
Can anyone please help me to correct the above method?
The error is that it is overly complicated.
/formatNumberString { % stack: number as a string
cvr % 1234567.8901
100 mul % get the relevant positions before the decimal point
.5 add % you wanted to round up always? 123456789.51 (else remove this line)
round cvi % 123456790
100 div % 1234567.9
.001 add % so we always have two digits after, the 1 will be cut off
100 string cvs % (1234567.901)
0 1 index length 1 sub % (1234567.901) 0 (length-1) cut off the last appended 1
getinterval % (1234567.90)
} def
The part around the 100 string
could be optimized.