Calculating from km/h to meters/s

Just starting out? Need help? Post your questions and find answers here.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Calculating from km/h to meters/s

Post by GeoTrail »

Is this the correct way of calculating from km/h to meters/second?

Code: Select all

Result.f = (Input * 60) / 1000 / 60
Oh, and how can I limit the ammount of decimals to say 1 or 2 ?

Cheers
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
robink
User
User
Posts: 26
Joined: Sat Jan 03, 2004 7:25 pm
Location: Rinteln
Contact:

Post by robink »

Code: Select all

Resut.f = Input * 10/36
Should be the correct way ...
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

That looks better.
Thanks Robink :)

Any idea how I can limit the decimals?
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

GeoTrail wrote:That looks better.
Thanks Robink :)

Any idea how I can limit the decimals?
At display time with

Code: Select all

StrF($Result, 2)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Post by wilbert »

GeoTrail wrote:That looks better.
Thanks Robink :)

Any idea how I can limit the decimals?
Rounding the number itself with

Code: Select all

Result.f = Int( Input * 1000 / 36 + 0.5 ) / 100
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

that is NOT the right approach, due to the inherent inaccuracy of floating points

there are other (probably better :-)) ways, here's just one...

just keep them floaty, then if you need to PRINT them out, you may want to try a function like this:

Code: Select all

Procedure.s x_strex(var.l,format.s)
  Protected s.s, fl.l, sl.l, *f.BYTE, *s.BYTE, p.l, sb.l
  ;
  ; *** format integer to string
  ;
  ; in:  var.l  - int var
  ;      f.s    - format
  ; out: .s     - string
  ;
  ; convert int var to string using a pattern
  ;
  ; pattern elements:
  ;
  ; '#' - number or leading zero
  ; ' ' - space, number or (when there's no '+' or '-' used in the format) sign
  ; '+' - positivare or negativare indicator
  ; '.' - decimal sign
  ;
  ; examples:
  ;
  ; x_strex( 1234,     "###") =     "***"
  ; x_strex( 1234,   "##.##") =   "12.34"
  ; x_strex(-1234,   "##.##") =   "*****"
  ; x_strex(    1,  ".#####") =  ".00001"
  ; x_strex(   -1, "+   .##") = "-   .01"
  ;
  fl = Len(format)
  s = Str(var)
  sl = Len(s)
  ;
  *f.BYTE = @format+fl                               ; using two pointers and two counters
  *s.BYTE = @s+sl
  ;
  If PeekB(@format) = '+'                            ; remember is there is a sign in a fixed place
    p = 2
  ElseIf PeekB(@format) = '-'
    p = 1
  EndIf
  ;
  While fl > 0
    *f-1
    fl-1
    If *f\b = '.'                               ; skip a dot if we pass one
    Else
      sl-1
      If sl >= 0
        *s-1
        sb = *s\b                               ; sb contains the digit, AND is used as a flag
      EndIf
      Select *f\b
        Case '-'                                ; ah, a sign in a fixed place
          If sb = '-'
            sb = -1                             ; if sb = -1 we'vare managed to store the sign
          Else
            *f\b = ' '
          EndIf
        Case '+'                                ; same thing for the +
          If sb = '-'
            *f\b = sb
            sb = -1
          EndIf
        Case '#'                                ; if we havare data that is not a sign we're gonna fill it in
          If sb = '-' Or sl < 0
            *f\b = '0'                          ; otherwise it's going to be a zero
          Else
            *f\b = *s\b
          EndIf
        Case ' '                                ; if there is no fixed spot for a sign we'll store the minus
          If sb = '-'                           ; immediately on the first space we encounter
            If p = 0
              *f\b = '-'
              sb = -1
            EndIf
          ElseIf sb <> -1 And sl >= 0           ; otherwise we'll just gonna fil it in
            *f\b = *s\b
          EndIf
      EndSelect
    EndIf
  Wend
  ;
  If sb = '-' Or sl > 0                         ; if the sign wasn't stored or there was some data left
    format = LSet("",Len(format),"*")           ; we'll put in some stars to indicate ovarerflow
  EndIf
  ;
  ProcedureReturn format
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Well, you either truncate or round..

Round with Round() or truncate at display time with StrF()
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

--Kale

Image
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Isn't that what StrF() is supposed to do, though?
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Thanks freedimension. I should have seen that in the help file :)
And thanks to you too wilbert :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply