Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Truncate float without rounding (https://www.delphipraxis.net/178390-truncate-float-without-rounding.html)

WojTec 5. Jan 2014 16:33

Delphi-Version: 5

Truncate float without rounding
 
How to truncate float number to n digits after point, but without rounding it, for example 147.258 --> 147.25 (input is extended) :wall:

Bernhard Geyer 5. Jan 2014 16:34

AW: Truncate float without rounding
 
For two digits:

Delphi-Quellcode:
myVal := 147.258;
myVal := Trunc(myVal*100)/100;

kamel08 5. Jan 2014 17:06

AW: Truncate float without rounding
 
// 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'

WojTec 5. Jan 2014 17:14

Re: Truncate float without rounding
 
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?

Furtbichler 5. Jan 2014 19:44

AW: Truncate float without rounding
 
The output will be extended same way. You have to convert it to an integer yourself.

Medium 5. Jan 2014 19:53

AW: Truncate float without rounding
 
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.)

sx2008 5. Jan 2014 19:55

AW: Truncate float without rounding
 
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;

WojTec 5. Jan 2014 22:00

Re: Truncate float without rounding
 
I know Int() :) IF we are here, could you explain me differences between Round(), Trunc() and Int()?

Sir Rufo 5. Jan 2014 22:06

AW: Re: Truncate float without rounding
 
Zitat:

Zitat von WojTec (Beitrag 1242316)
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? :roll:

Furtbichler 5. Jan 2014 22:07

AW: Truncate float without rounding
 
Look it up in google, I just did it and it worked (google) so it's not down and will help you same way.


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:25 Uhr.
Seite 1 von 2  1 2      

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