AGB  ·  Datenschutz  ·  Impressum  







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

Truncate float without rounding

Ein Thema von WojTec · begonnen am 5. Jan 2014 · letzter Beitrag vom 8. Jan 2014
Antwort Antwort
Seite 1 von 2  1 2      
WojTec

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

Truncate float without rounding

  Alt 5. Jan 2014, 16:33
Delphi-Version: 5
How to truncate float number to n digits after point, but without rounding it, for example 147.258 --> 147.25 (input is extended)

Geändert von WojTec ( 5. Jan 2014 um 17:00 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von Bernhard Geyer
Bernhard Geyer

Registriert seit: 13. Aug 2002
17.170 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Truncate float without rounding

  Alt 5. Jan 2014, 16:34
For two digits:

Delphi-Quellcode:
myVal := 147.258;
myVal := Trunc(myVal*100)/100;
Windows Vista - Eine neue Erfahrung in Fehlern.
  Mit Zitat antworten Zitat
kamel08
(Gast)

n/a Beiträge
 
#3

AW: Truncate float without rounding

  Alt 5. Jan 2014, 17:06
// For n digits:

Delphi-Quellcode:
Function RoundedReal(n:Double; count:Integer):Double;
// count: Anzahl der Stellen nach dem Komma
var multi:Double;
begin
     multi := IntPower(10, count);
     n := Round(n * multi);
     result := n / multi;
end;
// IntPower erfordert die Unit 'Math'

Geändert von kamel08 ( 5. Jan 2014 um 17:10 Uhr)
  Mit Zitat antworten Zitat
WojTec

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

Re: Truncate float without rounding

  Alt 5. Jan 2014, 17:14
Thanks you, now I understand how it working

BTW: if input is Extended and want to truncate to 0 digits (like Round), output should be what type? Cardinal?
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#5

AW: Truncate float without rounding

  Alt 5. Jan 2014, 19:44
The output will be extended same way. You have to convert it to an integer yourself.
  Mit Zitat antworten Zitat
Medium

Registriert seit: 23. Jan 2008
3.679 Beiträge
 
Delphi 2007 Enterprise
 
#6

AW: Truncate float without rounding

  Alt 5. Jan 2014, 19:53
For a totally general case, an Int64 would cover the largest range out of the integer types, that might be put in by an Extended. Cardinal most notably misses the sign. It depends on what you use the number for if you could use smaller types as output. (But even an Int64 can not cover the full range of an Extended by design - not without some rather wild re-mappings.)
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat
Benutzerbild von sx2008
sx2008

Registriert seit: 15. Feb 2008
Ort: Baden-Württemberg
2.332 Beiträge
 
Delphi 2007 Professional
 
#7

AW: Truncate float without rounding

  Alt 5. Jan 2014, 19:55
It seems that the Int()-function is rather unknown to most programmers.
Delphi-Quellcode:
Function RoundedReal(n:Double; decimalplaces:Integer):Double;
var multi:Double;
begin
  Assert(decimalplaces >=0);
  multi := IntPower(10, decimalplaces);
  Result := Int(n * multi) / multi;
end;
fork me on Github
  Mit Zitat antworten Zitat
WojTec

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

Re: Truncate float without rounding

  Alt 5. Jan 2014, 22:00
I know Int() IF we are here, could you explain me differences between Round(), Trunc() and Int()?
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#9

AW: Re: Truncate float without rounding

  Alt 5. Jan 2014, 22:06
I know Int() IF we are here, could you explain me differences between Round(), Trunc() and Int()?
If we are here, did you read the documentation about Round, Trunc, Int, Floor, Ceil?
Why I have a NO in my mind?
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#10

AW: Truncate float without rounding

  Alt 5. Jan 2014, 22:07
Look it up in google, I just did it and it worked (google) so it's not down and will help you same way.
  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 03:00 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