How to calculate the number of digits after the point?

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

How to calculate the number of digits after the point?

Post by mestnyi »

For example:

Code: Select all

f.f = 11.1234
On the Internet, write that it is impossible, right?

My attempts :)

Code: Select all

f.f = 11.1234
Debug StrF(f,4)
Debug StrF(f) 
 
For i=0 To 10
  If Left(StringField(StrF(f), 2, "."),i) <> StringField(StrF(f,i), 2, ".") Or
     Val(Left(Mid(StringField(StrF(f), 2, "."),i+1),1)) = 0
    Break
  EndIf
Next
 
Debug "Count "+i
Last edited by mestnyi on Fri Mar 17, 2017 10:02 pm, edited 2 times in total.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: How to calculate the number of digits after the point?

Post by Keya »

Im a bit confused by your question, but im wondering if it's the same thing i needed to do only last month which (carefully) trims zero chars off numbers, eg. 1.2300 -> 1.23

Code: Select all

;// Supports Ascii + Unicode

Procedure.s TrimZeros(s$)  ;trim trailing zero chars from a string, eg "1.2300" -> "1.23"
  flen = StringByteLength(s$)
  *pbuf.Ascii = @s$ + flen - SizeOf(Character)
  flen/SizeOf(Character)
  nullpts=flen
  For i = 1 To flen
    If *pbuf\a <> '0' ;And *pbuf\a <> '.' 
      *pbuf + SizeOf(Character)
      *pbuf\a = 0
      Break
    Else
      nullpts-1
    EndIf
    *pbuf - SizeOf(Character)
  Next i
  If nullpts<=0: s$ = "0": EndIf
  If Right(s$,1) = ".": s$ = Left(s$, Len(s$)-1): EndIf
  ProcedureReturn s$
EndProcedure

Procedure xTest(s$)
  Debug s$ + " = " + TrimZeros(s$)
EndProcedure

xTest("40.0")     ;40
xTest("1")        ;1
xTest("12")       ;12
xTest("12.")      ;12
xTest("12.0")     ;12
xTest("0")        ;0
xTest("0.000")    ;0
xTest("0.0")      ;0
xTest("1.123000") ;1.123
xTest("1.1230")   ;1.123
xTest("1.123")    ;1.123
xTest("1.0")      ;1
xTest("1.01")     ;1.01
xTest("1.010")    ;1.01
xTest("1")        ;1
xTest("123")      ;123
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to calculate the number of digits after the point?

Post by Marc56us »

One way (not the fastest)

String Transform
Find the point then delete it to the left and count
or
Look at the point and then count what remains on the right

Code: Select all

f.f = 11.1234

F$ = StrF(f,4)

j = Len(RemoveString(F$, Left(F$, FindString(F$, "."))))

Debug j ; 4
You can probably do better with pointers

To trim 0, same way

Code: Select all

Procedure xTest(s$)
     If FindString(s$, ".")
          t$ = RTrim(s$, "0")
          t$ = RTrim(t$, ".")
     Else
          t$ = s$
     EndIf
     Debug s$ + " -> " + t$
EndProcedure

xTest("40.0")     ;40
xTest("1")        ;1
xTest("12")       ;12
xTest("12.")      ;12
xTest("12.0")     ;12
xTest("0")        ;0
xTest("0.000")    ;0
xTest("0.0")      ;0
xTest("1.123000") ;1.123
xTest("1.1230")   ;1.123
xTest("1.123")    ;1.123
xTest("1.0")      ;1
xTest("1.01")     ;1.01
xTest("1.010")    ;1.01
xTest("1")        ;1
xTest("123")      ;123 
:wink:
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: How to calculate the number of digits after the point?

Post by mestnyi »

How to calculate the number of digits after the dot?
How can you explain.
I need to know how many digits are in the variable that I specified after the point?

Code: Select all

f.f = 11.1234

;f = ? - Decimals 

StrF(f,Decimals) 
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to calculate the number of digits after the point?

Post by Demivec »

mestnyi wrote:How to calculate the number of digits after the dot?
How can you explain.
I need to know how many digits are in the variable that I specified after the point?

Code: Select all

f.f = 11.1234

;f = ? - Decimals 

StrF(f,Decimals) 
You can only know the number of decimal digits if the value can be accurately represented by floating point (in binary).

The value in your sample code, 11.1234, is not accurately represented in floating point. So I would say the answer is impossible unless the floating point values are being restricted to only certain values which can be represented in floating point accurately.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to calculate the number of digits after the point?

Post by infratec »

What you want is not really possible.

A float variable in real live can have infinite digits after the point.
Example: 1/3 -> 0.3333333333333333333333333333333333333333333333333333333333333333....

How will you count them?

In a computing language a float is limited by the format of 4 used bytes. (14 digits after the point)

If you want the value of the digits which are limited, you have to count the digits until only 0 follows
Use StrF(float, 20) for this, than you are on the safe site :)

Code: Select all

Define a.f, i.i

For i = 0 To 1000
  Debug Str(i) + " " + StrF(a, 20)
  a + 0.1
Next i
Bernd
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: How to calculate the number of digits after the point?

Post by mestnyi »

Impossible is impossible. :D We will use a string.
I needed to set the increment and get from the increment decimal places
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: How to calculate the number of digits after the point?

Post by mestnyi »

What you want is not really possible.
The following code does something like what I needed.

Code: Select all

; Was in the message User_Russian
x.f = 11.1234 

y = Int(x)
z = (x-y)*10000

Debug y
Debug ""+Len(Str(z))+" - "+z
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How to calculate the number of digits after the point?

Post by dcr3 »

I dont't know if this is usefull.?

Code: Select all

Dim decimals.s(0)
;D=0
;NAD=NumbersAfterDecimals
NAD.s="11.1234567890123456789012345678901234567890123456789012345678901234567890123456789012"
If FindString(NAD,".")
FNAD = FindString(NAD,".")
decimals(D) = Trim(Right(NAD,Len(NAD)-FNAD))
For Count=0 To Len(decimals(D))-1
Next
            
Debug "NumbersCountAfterDecimal = "+count
EndIf
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: How to calculate the number of digits after the point?

Post by Zebuddi123 »

Code: Select all

NAD.s="11.1234567890123456789012345678901234567890123456789012345678901234567890123456789012"
x = Len(Mid(NAD, FindString(NAD, Chr(46), 1) + 1))  
Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How to calculate the number of digits after the point?

Post by dcr3 »

Zebuddi. Nice :lol: :lol:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to calculate the number of digits after the point?

Post by Dude »

All the above don't take into account that the decimal separator actually varies by country. A lot of countries use a comma instead, so you need to take that into account by setting up a global variable first:

Code: Select all

NAD.s="11.12345"
x = Len(Mid(NAD, FindString(NAD, Chr(46), 1) + 1))
Debug x ; 8 = Wrong for Denmark, Germany, Greece, Spain, Sweden, etc.

Global decsep$=Space(10)
GetLocaleInfo_(#LOCALE_USER_DEFAULT,#LOCALE_SDECIMAL,decsep$,9)

x = Len(Mid(NAD, FindString(NAD, decsep$, 1) + 1))
Debug x ; 5 = Correct for the above countries. :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: How to calculate the number of digits after the point?

Post by RASHAD »

Code: Select all

x.f = 11.123456
xstr$ = "11.123456"
nodec = Len(xstr$) - FindString(xstr$, ".")

y = Int(x)
z = (x-y)*Pow(10,nodec)

Debug y
Debug ""+Len(Str(z))+" - "+z
Egypt my love
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: How to calculate the number of digits after the point?[d

Post by mestnyi »

What are you doing here? :D
[
RASHAD wrote:

Code: Select all

x.f = 11.123456
xstr$ = "11.123456"
nodec = Len(xstr$) - FindString(xstr$, ".")

y = Int(x)
z = (x-y)*Pow(10,nodec)

Debug y
Debug ""+Len(Str(z))+" - "+z
xstr$ = "11.123456"
this is what from where the string is not known? :) :) :)

Here's what I had in mind: I have 6 digits after the dot sign

Code: Select all

x.f = 11.157498

Procedure.l my(param.f)
  Protected y.i = Int(param)
  Protected z = (param-y)*1000000
  ProcedureReturn z
EndProcedure

z=my(x)
Debug ""+Len(Str(z))+" - "+z
Post Reply