Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shortening Decimal Values - But not using _Round() 2

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
0
0
GB
Hi, i have some decimals auto-generated, they look something like this:

29.4117647058824
31.1418685121107
34.6020761245675


anyway, how can i trim/delete/shorten the numbers to look like:

29.4
31.1
34.6


Thanks
 
with rounding:

Code:
function LimitDecimals(f : Extended; Decimals : Integer) : Extended;
begin
 Result := Round(f* Power(10, Decimals)) / Power(10, Decimals);
end;

without rounding:

Code:
function LimitDecimals(f : Extended; Decimals : Integer) : Extended;
begin
 Result := Trunc(f* Power(10, Decimals)) / Power(10, Decimals);
end;

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
also look at format string.

float := 29.4117647058824;
mystring := Format('%*.*f', [8, 1, float]);

Aaron
 
I like the Format method. You could also use FloatToStrF.
 
I like the Format method. You could also use FloatToStrF.

well it depends on the fact that the output must be a string value or a floating point value. The OP did'nt specify this, so I assume we are talking about floating point return values and not strings.
also remember that the format function rounds the value and maybe, this is not desired.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
yes, sorry it does round (didnt know that).

29.4117647058824 came out 29.4
but
29.4997647058824 came out 29.5

this works
Code:
var float: double;
var posi: integer;
begin
float := 29.4997647058824;
//find the point
posi := pos('.', floattostr(float));
//copy from start to 1 more than point
caption := copy(floattostr(float),0,posi+1);
end;

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top