AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Count digits after point

Ein Thema von WojTec · begonnen am 15. Feb 2014 · letzter Beitrag vom 15. Feb 2014
Antwort Antwort
Seite 1 von 2  1 2      
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Count digits after point

  Alt 15. Feb 2014, 15:10
Delphi-Version: 2010
I need to know how many digits has float number after point.

Delphi-Quellcode:
function CountDigitsAfterPoint(AValue: Extended): Byte;
begin
  Result := 0;

  while (Frac(AValue) <> 0) do
  begin
    Inc(Result);
    AValue := AValue * 10;
  end;
end;
Working good in most cases, but not for all

Incorect results for sample data:
2.048 --> 19
8.448 --> 19
565.148 --> 17
32.064 --> 18
274.176 --> 17
97.728 --> 17
155.52 --> 17
622.08 --> 17



Please help
  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#2

AW: Count digits after point

  Alt 15. Feb 2014, 15:29
That is because there are some inaccuracy regarding non-integer numbers.
0.99999999999999999999.... is equal to 1!
(or your example: 2.048 could internally be 2.04799999999999999...)
Therefore your method needs to take that into account.
But sadly I have no idea since my knowledge about precision is not good. Someone else might be of help!

Edit: Another problem that came to mind is irrational numbers. I thought Id mention it but it shouldnt be too problematic since the data-type cuts off after a certain degree of precision..
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG

Geändert von Aphton (15. Feb 2014 um 15:36 Uhr)
  Mit Zitat antworten Zitat
DelphiProgrammer

Registriert seit: 5. Apr 2013
Ort: Kennste eh net! wer weiss, wer weiss o.o
43 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#3

AW: Count digits after point

  Alt 15. Feb 2014, 15:32
Hi,
maybe that'll help:
Code:
decimalplaces := (length(floattostr(number)) - pos(',',floattostr(number))-1);
Christopher
---------------------------------------------------
I ♥ Delphi
  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#4

AW: Count digits after point

  Alt 15. Feb 2014, 15:41
@DelphiProgrammer:
Nope!

Delphi-Quellcode:
var
  x: Single;

begin
  x := 2.048;
  writeln(x);
  writeln((length(floattostr(x)) - pos('.',floattostr(x))-1));
  readln;
end.
Output
Code:
 2.04800009727478E+0000
15
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: Count digits after point

  Alt 15. Feb 2014, 15:51
I thought about strings too, but (1) it's lame and (2) see @Aphton's post.

There is no math solution?
  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#6

AW: Count digits after point

  Alt 15. Feb 2014, 16:07
This might be a problem that can be solved in another way.
You want to find out the number of fraction-digits.
Why?
Maybe the root problem you are trying to solve can be solved in another way! Maybe you dont even need to find out the digitcount!
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.851 Beiträge
 
Delphi 11 Alexandria
 
#7

AW: Count digits after point

  Alt 15. Feb 2014, 16:09
Why you need the number of digits? perhaps there is a better way to achieve your aim.
Markus Kinzler
  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#8

AW: Count digits after point

  Alt 15. Feb 2014, 16:10
@mkinzler: xDD
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#9

Re: Count digits after point

  Alt 15. Feb 2014, 16:50
So, reason is not important. Important is problem.
But if you want to know: (1) result is required for further computation purposes and (2) for formating.
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.851 Beiträge
 
Delphi 11 Alexandria
 
#10

AW: Count digits after point

  Alt 15. Feb 2014, 16:56
For (1) because the system iminent computation error the consumption more digits are better doesn't seem right to me.
For (2) I'd prefer to restrict the digits to dispay to a fixed count
Markus Kinzler
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:42 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz