Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Making Real Numbers To 2 Decimal Places?


  • Please log in to reply
4 replies to this topic

#1 HelpMePlz

HelpMePlz

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 26 April 2012 - 10:56 AM

I want to make a labels caption a real number, but I want to have it show only 2 decimal places. E.g 2.22.

I am trying to do this using the following code:
PercentChangeText.Caption := (FloatToStr((PercentageChange):0:2) + '%');

It works fine whenever I am writing real numbers to a file but for some reason when I try and use this code I get the following error:

Syntax error; ')' expected but ':' found


Any idea how to fix this?
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 April 2012 - 02:59 PM

Use FormatFloat, instead.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 26 April 2012 - 07:16 PM

For this case, my personal preference would be to use Format function. Like this:

uses
  ..
  , SysUtils
  , ..
  ;

  PercentChangeText.Caption := Format('%.2f%%', [PercentageChange]);

It's easier to inspect what the result will be and supports formatting multiple values.

On your coding style, I see there were many redundant parentheses. The outermost ones were not needed. Also the innermost ones (those that surround PercentageChange). When not really needed, parentheses tend to block the flow of thought of the reader. When readers' eyes hit a parenthesis (especially the closing one), their mind will start finding out which group the parenthesis defined.

Nothing technically bad about it, though.
  • 0

#4 HelpMePlz

HelpMePlz

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 27 April 2012 - 06:33 AM

For this case, my personal preference would be to use Format function. Like this:

uses
  ..
  , SysUtils
  , ..
  ;

  PercentChangeText.Caption := Format('%.2f%%', [PercentageChange]);

It's easier to inspect what the result will be and supports formatting multiple values.

On your coding style, I see there were many redundant parentheses. The outermost ones were not needed. Also the innermost ones (those that surround PercentageChange). When not really needed, parentheses tend to block the flow of thought of the reader. When readers' eyes hit a parenthesis (especially the closing one), their mind will start finding out which group the parenthesis defined.

Nothing technically bad about it, though.


I tried that but it doesn't seem to work :/ the program still runs fine but the real number is still displayed to like 100 decimal places :/
  • 0

#5 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 27 April 2012 - 07:52 AM

It's working just fine with my Delphi 7. Check out the attached sample code and its executable. I think the source code will compile just fine in Turbo Delphi.

Attached Files


  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download