In PB it appears that the Round() only rounds to Integers. So is the PB way to round to say 2 decimals places the following?
Code: Select all
lnCost.f = Round(10.4567*100,#PB_Round_Up)/100 ; answer should be 10.46
Simon
Code: Select all
lnCost.f = Round(10.4567*100,#PB_Round_Up)/100 ; answer should be 10.46
Code: Select all
incost.f = 10.426598745
Rstr$ = StrF(incost.f,2)
MessageRequester("VALUE", ""+Rstr$)
newflot.f = valf(Rstr$)
messagerequester("OUT", "Float reports: "+strf(newflot.f))
Code: Select all
;==========================================================
;SUBROUTINE TO CONVERT X$ TO MONEY STRING FORMAT USING TWO
;DECIMAL PLACES
;This subroutine ensures that the string does not contain
;any invalid characters outside the following :-
;0-9, ".", "-". Note that the minus sign is only permitted
;in the first character position. The decimal point must
;be expressed as a period character "." although it is
;easy to modify this subroutine and substitute the use of
;a comma instead of a period in countries where this need
;arises.
;
;Supplied - X$ Normally this would be a user entered value
;
;Returned - X$ Validated and rounded to two decimal places
; Invalid data will be indicated when X$
; contains the text "ERROR" instead of a
; formatted numeric string value.
;=========================================================
MONEYSTRING:
;Check if user entered zero in any form (which is OK)
If X$="0.00" Or X$="0.0" Or X$="0" Or Len(X$) < 1
X$="0.00"
Return
EndIf
;Make sure the string value is not any form of negative zero
If Left(X$,1)="-" And ValD(X$)=0
X$="ERROR"
Return
EndIf
;When first character is "." add a leading "0" to the string
If Left(X$,1)="."
X$="0"+X$
EndIf
;Make sure the string value is not "-."
If X$="-."
X$="ERROR"
Return
EndIf
;If string has length >2 and starts with "-.",
;make it start with "-0." instead
L=Len(X$)
If L > 2
If Left(X$,2)="-."
JCSPART1$=Left(X$,2)
JCSPART2$=Right(X$,(L-2))
X$="-0."+JCSPART2$
EndIf
Endif
;Check string only contains acceptable characters
;and not more than one "-" or "."
;Return ERROR if any invalid characters are found
;First pass through the string
L=Len(X$)
JCSXPERIOD=0
For JCSI = 1 to L
Result$=Mid(X$,JCSI,1)
;If a Dash is located, only allow it to appear in first character position
If Result$="-" And JCSI > 1
X$="ERROR"
Return
EndIf
;Only allow valid characters 0-9 and "."
If Result$ = "." Or Result$ = "-" Or (Result$ >="0" And Result$ <="9")
X$=X$
Else
X$="ERROR"
Return
Endif
;Count the number of periods entered.
;Return ERROR If more than one are found
If Result$="."
JCSXPERIOD=JCSXPERIOD+1
EndIf
If JCSXPERIOD >1
X$="ERROR"
Return
EndIf
Next JCSI
;Fix what comes after the decimal point if needed
;Also fix where there is no decimal point present
;Where string has no decimal point, then add ".00" to the end
If FindString(X$,".",1) = 0
X$=X$+".00"
Return
EndIf
;If string ends with a decimal point, add "00" to the end
If Right(X$,1)="."
X$=X$+"00"
Return
EndIf
;If string has one numeric after the decimal, add "0" to the end
L = Len(X$)
If Mid(X$,(L-1),1)="."
X$=X$+"0"
Return
EndIf
;Ignore cases where the decimal point is positioned correctly
L = Len(X$)
If Mid(X$,(L-2),1)="."
Return
EndIf
;Fix cases where more than two digits appear after the decimal point
;First, find position of the decimal point within the string
;If there are more than two chrs after decimal point, then eliminate
;the excess after rounding up or down for the 3rd digit taking account
;of handling both negative and positive values
XLength = FindString(X$,".",1)
;Perform rounding if needed
If Len(X$) > XLength+2
If ValD(X$) > 0
X.d=ValD(X$)+0.005
ElseIf ValD(X$) < 0
X.d=ValD(X$)-0.005
EndIf
X$=StrD(X.d)
XLength = FindString(X$,".",1)
EndIf
If Len(X$) > XLength+2
X$=Left(X$,XLength+2)
Return
EndIf
;Everything else is wrong
X$="ERROR"
RETURN