Solved

Getting only 2 numbers after the decimal.

Posted on 2004-08-12
12
187 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
Comment Utility
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
Comment Utility
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
Comment Utility
try (just from head):
  Fields.FieldByName('VALOR_ICMS').DisplayFormat := '#.00';
will only show as 2.58,
but will know as 2.5758
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
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
Comment Utility
I was goinf to say the same as DavidBirch's first comment. The simplest way is best.
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
 (Fields.FieldByName('VALOR_ICMS') as TFloatField).DisplayFormat := '0.#0';
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Expert Comment

by:krypto2000
Comment Utility
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
Comment Utility
isnt messing around with strings quite a slow way of acheiveing this?

DAvid
0
 

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:DavidBirch2dotCom
Comment Utility
Thanks for the points :)

David
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now