Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Single und FloatToStr (https://www.delphipraxis.net/138556-single-und-floattostr.html)

DelphiManiac 12. Aug 2009 13:56


Single und FloatToStr
 
Hallo,

ich habe einen Single (IEE754 einfache Genauigkeit) Fließkommawert den ich anzeigen will.

Jetzt kann die Funktion FloatToStr ja einen Extended aufnehmen und akzeptiert auch Double / Single Werte.

Mein Problem ist aber dass wenn ich einen Wert beispielweise:

22.1 (Single)
habe dieser über FloatToStr zu 22.10000000381469727 wird (Doppelte Genauigkeit)

Gibt es eine Convertfunktion die mir nur den Singlewert betrachtet? Oder muss ich dann wieder runden?!

Vielen Dank

Gruß
DelphiManiac

Klaus01 12. Aug 2009 14:01

Re: Single und FloatToStr
 
Hallo,

kannst Du nicht FlaotToStrF nutzen:

Zitat:

FloatToStrF converts the floating-point value given by Value to its string representation.
The Value parameter is the value to convert.
The Precision parameter specifies the precision of the given value.
It should be 7 or less for values of type Single, 15 or less for values of type Double, and 18 or less for values of type Extended.
Grüße
Klaus

DelphiManiac 12. Aug 2009 14:02

Re: Single und FloatToStr
 
Hi,

doch das könnte ich tun,
meine Frage jetzt noch Precision ist ja 7, das ist ok, aber was soll ich für ein Zahlenformat nehmen und wie viele Nachkommastellen, denn das weiß ich ja vorher nicht?!

Danke

Zoot 12. Aug 2009 14:10

Re: Single und FloatToStr
 
Wie viele Stellen willst du denn sehen?
Delphi-Quellcode:
FormatFloat ('0.00',x)
oder so ähnlich.

Klaus01 12. Aug 2009 14:12

Re: Single und FloatToStr
 
Du könntest es mal mit ffFixed probieren.

Zitat:

Zitat von DelphiHilfe
ffFixed:
Fixed point format. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative, and at least one digit always precedes the decimal point. The number of digits after the decimal point is given by the Digits parameter--it must be between 0 and 18. If the number of digits to the left of the decimal point is greater than the specified precision, the resulting value will use scientific format. The resulting values is padded with zeros when the Digits parameter is higher than the number of digits dictated by precision. For example, given ffFixed with Precision set to 5 and digits set to 3, fomatting 345.6789 gives the string '345.680', padding with zeros after the precision is met.

Grüße
Klaus

TeronG 12. Aug 2009 14:20

Re: Single und FloatToStr
 
Zitat:

Zitat von DelphiHilfe
ffFixed:
Festkommaformat. Der Wert wird in einen String der folgenden Form umgewandelt: "-ddd.ddd...". Der resultierende String beginnt mit einem Minuszeichen, wenn es sich um eine negative Zahl handelt. Vor dem Dezimaltrennzeichen steht immer mindestens eine Ziffer. Die Anzahl der Stellen nach dem Dezimalkomma wird über den Parameter Digits angegeben. Es muss sich dabei um eine Zahl zwischen 0 und 18 handeln. Ist die Anzahl der Stellen links vom Dezimalkomma größer als die angegebene Genauigkeit, wird für den resultierenden Wert das wissenschaftliche Format verwendet. Die resultierenden Werte werden mit Nullen aufgefüllt, wenn der Parameter Digits größer ist als die Anzahl der durch Precision angegebenen Stellen. Wenn beispielsweise ffFixed mit dem Wert 5 für Genauigkeit (Precision) und 3 Stellen angegeben wird, dann ergibt die Formatierung von 345.6789 den String '345.680', der mit Nullen aufgefüllt wird, nachdem die geforderte Genauigkeit erreicht wurde.

btw: (7 Signifikante Stellen) ungleich (7 Nachkommastellen)


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:05 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