Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Getting only 2 numbers after the decimal.

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
fabyola
Asked:
fabyola
  • 5
  • 3
  • 2
  • +2
1 Solution
 
DavidBirch2dotComCommented:
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
 
DavidBirch2dotComCommented:
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
 
esoftbgCommented:
try (just from head):
  Fields.FieldByName('VALOR_ICMS').DisplayFormat := '#.00';
will only show as 2.58,
but will know as 2.5758
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mokuleCommented:
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
 
HorridManCommented:
I was goinf to say the same as DavidBirch's first comment. The simplest way is best.
0
 
esoftbgCommented:
 (Fields.FieldByName('VALOR_ICMS') as TFloatField).DisplayFormat := '0.#0';
0
 
krypto2000Commented:
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
 
DavidBirch2dotComCommented:
isnt messing around with strings quite a slow way of acheiveing this?

DAvid
0
 
krypto2000Commented:
sorry I don't speak very well english.. What do you say ? it is slowly ?...
0
 
DavidBirch2dotComCommented:
are not string functions more slow than doing it with numbers ?
0
 
krypto2000Commented:
Yes you have reason.... I've making some test and your function is 35% faster than my...
0
 
DavidBirch2dotComCommented:
Thanks for the points :)

David
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now