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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another number round off question. 1

Status
Not open for further replies.

NewToProgramming

Technical User
Jun 24, 2003
31
US
I have a math equation that gives me the answer in a edit box. I want to have the number to only have the tenths in the decimal area, but not rounded off, just chopped off.
example: Say the number is 56.4589. Rounded off to the tenths digit would be 56.5. I don't want that. I want it to just remove anything after the tenths digit giving the answer of 56.4.
Any ideas?
Thank you,
Mark
 
making this up on the spot:

edtBox.Text := RoundToStrF(fltNumber, ffFixed, 18, 1);

Not sure about the 18, might want to check, the documentation isn't too helpfull about exactly what it does.
 
try Trunc():

Truncates a real number to an integer.

Unit

System

Category

arithmetic routines

function Trunc(X: Extended): Int64;

Description

The Trunc function truncates a real-type value to an integer-type value. X is a real-type expression. Trunc returns an Int64 value that is the value of X rounded toward zero.

If the truncated value of X is not within the Int64 range, an EInvalidOp exception is raised.

 
If you want a decimal place with the above, try

(Trunc(NumberIn*10))/10
 
Thank you KempCGDR.
I used your (Trunc(NumberIn*10))/10 and it works great.
Mark
 
Another method you could use is the RoundTo routine as it returns a double rather than an integer. The only annoyance is that you have to set the rounding mode to truncate before you use it. The second parameter of the routine indicates the power of ten to which you want the value rounded.
Code:
  SetRoundMode(rmTruncate);
  ShowMessage(FloatToStr(RoundTo(56.4589, -2)));

Hope this helps!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top