Solved

Getting only 2 numbers after the decimal.

Posted on 2004-08-12
12
198 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
[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
  • Learn & ask questions
  • 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
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!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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