Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Getting only 2 numbers after the decimal.

Posted on 2004-08-12
Medium Priority
207 Views
I have a currency like: 2.5758 I need to get all of the numbers before the decimal and only two after the decimal. I tried

FormatFloat('###.##',Fields.FieldByName('VALOR_ICMS').AsFloat) );
and
FormatCurr('0.00', Fields.FieldByName('VALOR_ICMS').AsFloat)

but it rounds to '2.58' and I donĀ“t want it to round. In other words. I want to format my currency number without rounding it..
0
Question by:fabyola
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +2

LVL 7

Accepted Solution

DavidBirch2dotCom earned 200 total points
ID: 11787681
try this

function Cutdecimals(mynum: float): float;
begin

If mynum = 0 then
begin
result:= 0;
exit;
end;

mynum:= mynum*100;
mynum:= Trunc(mynum);
result:= mynum / 100;
end;

David

0

LVL 7

Expert Comment

ID: 11787809
or this

function Cutdecimals(mynum: float; decimals: integer): float;

function Exponent(num: integer; Exponent: Integer): integer;
begin
Result := 1;

while Exponent < 0 do
begin
Result := Result / num;
Exponent := Exponent + 1;
end;

while Exponent > 0 do
begin
Result := Result * num;
Exponent := Exponent - 1;

end;
end;

begin

If mynum = 0 then
begin
result:= 0;
exit;
end;

mynum:= mynum*Exponent(10,decimals);
mynum:= Trunc(mynum);
result:= mynum /Exponent(10,decimals);
end;

just pass the number you want to format and the number of places to this function

David
0

LVL 12

Expert Comment

ID: 11788678
Fields.FieldByName('VALOR_ICMS').DisplayFormat := '#.00';
will only show as 2.58,
but will know as 2.5758
0

LVL 17

Expert Comment

ID: 11788807
What do You think anout this

function CutDecimal(str: string):string;
var
p: integer;
begin
p := Pos(DecimalSeparator,str);   // where is decimal point
if p >= 0 then
result := Copy(str,1,p+2)           // take 2 after decimal
else
result := str;                            // no decimal take everything
end;
0

Expert Comment

ID: 11789485
I was goinf to say the same as DavidBirch's first comment. The simplest way is best.
0

LVL 12

Expert Comment

ID: 11790744
(Fields.FieldByName('VALOR_ICMS') as TFloatField).DisplayFormat := '0.#0';
0

Expert Comment

ID: 11819009
I would make it like that :

================================================
uses strUtils;

function formatNumber(num: real): real;
begin
result := strToFloat(leftStr(floatToStr(num), pos('.',floatToStr(num))+2));
end;
================================================
0

LVL 7

Expert Comment

ID: 11819021
isnt messing around with strings quite a slow way of acheiveing this?

DAvid
0

Expert Comment

ID: 11819212
sorry I don't speak very well english.. What do you say ? it is slowly ?...
0

LVL 7

Expert Comment

ID: 11819409
are not string functions more slow than doing it with numbers ?
0

Expert Comment

ID: 11819698
Yes you have reason.... I've making some test and your function is 35% faster than my...
0

LVL 7

Expert Comment

ID: 11891451
Thanks for the points :)

David
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small printā¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toā¦
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vulnā¦
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signalā¦
###### Suggested Courses
Course of the Month10 days, 18 hours left to enroll