decimal places

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by willinyork.

Hi! Thanks for all your help, it worked like a charm :)

However I am stuck on one last thing before I can finish my program :) Is there any way of doing this with numbers that have a decimal point in them? Basically, I have 2 string gadgets for the user to enter a value, a button to perform a calculation, and another string gadget to display the answer. If you type in 125 in the first gadget (gadget 50), then 4 in the second gadget (gadget 51) and click the button, it should return a value of 1.92, but doing it like this return 2 (the nearest whole number).

tempobpm.l=Val(GetGadgetText(50))
beats.l=Val(GetGadgetText(51))
length.f=((60/tempobpm)*beats)
length$=Str(length)
SetGadgetText(52, length$)

I would like to have the answer displayed at least 2 decimal places, preferably 3 if possible. Is this the right way to go about things? A friend of mine mentioned something about floating points, so I don't know whether I'm missing something obvious or not :) Thanks!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by wavemaker.

Try this (change gadget index to suit your needs):

Code: Select all

#ES_NUMBER = $2000
InitGadget(53)
If OpenWindow(0,0,0, 185, 172, #PB_Window_SystemMenu,"Calculate")
  If CreateGadgetList(WindowID())
    number = StringGadget(50, 30, 10, 110, 21, "")
    SetWindowLong_(number, #GWL_STYLE, GetWindowLong_(number, #GWL_STYLE)|#ES_NUMBER)
    number = StringGadget(51, 30, 40, 110, 21, "")
    SetWindowLong_(number, #GWL_STYLE, GetWindowLong_(number, #GWL_STYLE)|#ES_NUMBER)
    ButtonGadget(1, 46, 80, 80, 21, "Calculate")
    number = StringGadget(52, 30, 110, 110, 21, "")
    SetWindowLong_(number, #GWL_STYLE, GetWindowLong_(number, #GWL_STYLE)|#WS_DISABLED)
  EndIf 
EndIf   
Repeat 
  EventID.l = WaitWindowEvent()
  If EventID = #PB_EventGadget
   
     Select EventGadgetID()
       Case 3
         length.f = ((60/Val(GetGadgetText(50)))*Val(GetGadgetText(51)))
         SetGadgetText(52, StrF(length))
     EndSelect
     
   EndIf
Until EventID = #PB_EventCloseWindow
End
The key is the undocumented StrF function, which is the equivalent to Str to convert floating point numbers to strings in PureBasic. The fact that it is undocumented may be due to a buggy behavior in some circumstances, but I think that if use normal values it will work perfectly.

Bye,

Juan Calderón Alonso
Registered user
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Yes the StrF function is buggy (was still at v2.6a - didn't check it on 2.7...).

But Don helped me once. Go to search and type in:
StrF
and you will find a nice working solution for this.



Have a nice day...
Franco
Post Reply