Solved

Getting only 2 numbers after the decimal.

Posted on 2004-08-12
12
193 Views
Last Modified: 2010-04-05
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
Comment
Question by:fabyola
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 7

Accepted Solution

by:
DavidBirch2dotCom earned 50 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

by:DavidBirch2dotCom
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

by:esoftbg
ID: 11788678
try (just from head):
  Fields.FieldByName('VALOR_ICMS').DisplayFormat := '#.00';
will only show as 2.58,
but will know as 2.5758
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:mokule
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

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

Expert Comment

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

Expert Comment

by:krypto2000
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

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

DAvid
0
 

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:DavidBirch2dotCom
ID: 11891451
Thanks for the points :)

David
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question