[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Formating Numbers

Posted on 1997-07-10
13
Medium Priority
?
350 Views
Last Modified: 2010-04-06
How do I format numbers in a label with commas, dollar sign, or percentages?  For example, I want the number 1200 to appears are either

1,000
$1,200.00
1200%

Code examples would be most useful.
0
Comment
Question by:d4jaj1
[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
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 1

Expert Comment

by:kevinb062397
ID: 1338607
This will add the commas every 3rd char:
var
tempsize:string;
begin
tempsize:=<string>
   if length(<string>) > 3 then
   insert(',',tempsize,length(tempsize)-2);

   if length(<string>) > 6 then
   insert(',',tempsize,length(tempsize)-6);

   if length(<string>) > 9 then
   insert(',',tempsize,length(tempsize)-10);

To add the decimal:
//adjust the values in the code above if you use this. add 2 to //each value.
insert('.',<string>,length(string)-2);

To add the '$':
string := '$' + string;

To add the '%':
string := string + %;
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338608
I apologize for being vague with my question.  My problem really is formating numbers (not strings) in their correct type.  For example, my code sums the number of values in a database, which happen to be currency.  If the count returns a total of $12.78, the display in my label is 13!  How do I make the label show $12.78 instead of $13.00 -- which is what your example would produce?
0
 
LVL 1

Expert Comment

by:kevinb062397
ID: 1338609
This might be what you are looking for.

var
   S, T: string;
begin
   Str(1.4:2:1, T);
   S := T + ' rounds to ' + IntToStr(Round(1.4)) + #13#10;
   Str(1.5:2:1, T);
   S := S + T + ' rounds to ' + IntToStr(Round(1.5)) + #13#10;
   Str(-1.4:2:1, T);
   S := S + T + ' rounds to ' + IntToStr(Round(-1.4)) + #13#10;
   Str(-1.5:2:1, T);
   S := S + T + ' rounds to ' + IntToStr(Round(-1.5));
   MessageDlg(S, mtInformation, [mbOk], 0);
end;

If not let me know...

What label are you using?
Most labels only understand strings, therefore if you read a real number convert it to a string add a '.' between length(string)-2 and length(string)-3. You would have a decimal value to 2 places.  Adding the ','s to every 3rd position to the left of the decimal would give me standard notation.

If what you are looking four is how to change the notation in the db field then look at the Database Desktop.  There are options there for setting the "Money" Field.


Let me know how this works.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:d4jaj1
ID: 1338610
Now I'm confused. I though you were going to provide me with information on the CurrencyString or CurrencyFormat constants.  All I'm trying to do is some up numbers from a DBGrid and display them to a user.  I really don't care if it's a label, db field, etc.  How can I do this?
0
 

Expert Comment

by:danGynn
ID: 1338611
Okay, if you have a real number all you have to do to display it as a string with formatting is use the FormatFloat(string, real) procedure.  If your num is the real value 12.78 then to display it as a string simply use:
FormatFloat('$#,##0.00', num) ;


Hope this is what you are looking for.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338612
Sorry for the delay, I've had trouble upgrading to D3 and finally had to re-install D2.

Having said that, I'm having a problem with your solution.  It compiles okay, but doesn't give me the results I'm looking for, which is displaying the sum of numbers from a table to the user in a specific format.  I really don't care if its a text box, a calculated field, etc.

I have included my code below.  The number is sumed correctly, but it is still in integer format.  Can you tell me what I'm doing wrong?

var
Total: Integer;
begin
Total := 0;
tblDateHist.disablecontrols;
tblDateHist.first;
while not tblDateHist.EOF do
  begin
    Total := Total + tblDateHist.FieldByName('$ Spent').AsInteger;
     tblDateHist.next;
  end;
tblDateHist.enablecontrols;
FormatFloat('$#,##0.00', Total);
Spent.Caption := Inttostr(Total);
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338613
I don't understand why this is so difficult!  All I want to do to total a currency filed in a tabel and display it to the user.  My code is above.
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 80 total points
ID: 1338614
I think you should write:
Total := Total + tblDateHist.FieldByName('$ Spent').AsFloat;
This is not an Integer field, Am I right?
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338615
Didn't work.  Got an error stating - 'Incompatable Types - Integer and Real'.  I also tried to change Total to Currency in the VAR section and removed the 'Inttostr' at the end where I try to place the value in the label and got this error - 'Incompatible Types - string and Currency'.  Any suggestions?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338616
I don't understand, is the '$Spent' field is declared in the table as currency field?
If so, then you should use AsCurrency instead of AsInteger.
If not so, then how is it declared?
Beside, You should declare the 'Total' variable as Double and not as Integer.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338617
Okay, I changed the type of Total to Double and added the AsCurrency and stil recieve an error - Incompatable Types - Double and string.  '$ Spent' is a Paradox Money ($) field type.  An excerpt of my latest code is below;


var
Total: Double;
begin
Total := 0;
tblDateHist.disablecontrols;
tblDateHist.first;
while not tblDateHist.EOF do
  begin
    Total := Total + tblDateHist.FieldByName('$ Spent').AsCurrency;
    tblDateHist.next;
  end;
tblDateHist.enablecontrols;

FormatFloat('$#,##0.00', Total);
Spent.Caption := Total;   //This is were I get the above error.
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338618
That is becuase you should write:
Spent.Caption := FormatFloat('$#,##0.00', Total);
Caption and Float are differenet types (basic stuff).
Format of something always returns string.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338619
That was it.  Thanks.  I realized they were of different types, I just didn't know how to convert the float to a string and display it.

From the questions I ask, it is obvious I don't know a lot about Delphi.  That's because I'm trying to re-write a Paradox application for distribution.  The folks at Borland said, "it would be 'really' easy to do with Delphi, all I had to do was re-create the forms and Pascal is soooo easy, you don't have to had ever written code".  Of course they lied, but there's only so much I can learn from a book in two & a half  months (I started in June).  That's why I ask you experts my silly little questions.
0

Featured Post

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.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 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