RoundNumber()

Share your advanced PureBasic knowledge/code with the community.
akee
Enthusiast
Enthusiast
Posts: 479
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

RoundNumber()

Post by akee »

Just sharing the old Round() function found in Clipper programming that is different from PureBasic's version.
Included is an example of a data point graphing application setup.

Code: Select all

Procedure.d RoundNumber(arg_Number.d, arg_Decimals = 0)
  Protected arg_Power
  arg_Power = Pow(10, Abs(arg_Decimals))
  If arg_Decimals > 0
    arg_Number = arg_Number * arg_Power
  ElseIf arg_Decimals < 0
    arg_Number = arg_Number / arg_Power
  EndIf
  arg_Number = IntQ(arg_Number)
  If arg_Decimals > 0
    arg_Number = arg_Number / arg_Power
  ElseIf arg_Decimals < 0
    arg_Number = arg_Number * arg_Power
  EndIf
  ProcedureReturn arg_Number
EndProcedure

;=== Example 1
number.d = 562621.3728222
For a = -5 To 5
  Debug Str(a) + " : " + StrD(RoundNumber(number, a), 7)
Next


Debug ""


;=== Example 2
;=== Finding canvas scale for plotting data points.

Global datapoints_x_min.f, datapoints_x_max.f, datapoints_y_min.f, datapoints_y_max.f
Global canvas_x_min.f, canvas_x_max.f, canvas_y_min.f, canvas_y_max.f
Global canvas_width, canvas_height


;=== 
; find_lowest_and_highest_xy_datapoints
;===
datapoints_x_min = -167.726
datapoints_x_max = 242.13
datapoints_y_min = -55.78
datapoints_y_max = 155.78

; Round to the nearest 100.
canvas_x_min = RoundNumber(datapoints_x_min - 100, -2)
canvas_x_max = RoundNumber(datapoints_x_max + 100, -2)
canvas_y_min = RoundNumber(datapoints_y_min - 100, -2)
canvas_y_max = RoundNumber(datapoints_y_max + 100, -2)

canvas_width = canvas_x_max - canvas_x_min
canvas_height = canvas_y_max - canvas_y_min

Debug StrF(datapoints_x_min, 3) + " - " + StrF(datapoints_x_max, 3) + " : " + StrF(datapoints_y_min, 3) + " - " + StrF(datapoints_y_max, 3)
Debug StrF(canvas_x_min, 3) + " - " + StrF(canvas_x_max, 3) + " : " + StrF(canvas_y_min, 3) + " - " + StrF(canvas_y_max, 3)
Debug Str(canvas_width) + " x " + Str(canvas_height)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: RoundNumber()

Post by Kwai chang caine »

Can be usefull
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply