Recent

Author Topic: [SOLVED]Express real to only x decimal places?  (Read 952 times)

JamesSci

  • New Member
  • *
  • Posts: 30
[SOLVED]Express real to only x decimal places?
« on: March 23, 2020, 07:51:26 pm »
Hi!

I have this simple program:

Code: Pascal  [Select][+][-]
  1. program SalaryCalculator;
  2.  
  3. const
  4.   DollarsPerHour: real = 15.50;
  5.  
  6. var
  7.   HoursPerDay, DaysPerMonth, TotalHours: integer;
  8.   Salary: real;
  9.  
  10. begin
  11.   WriteLn('Please enter the number of hours you work in a day:');
  12.   ReadLn(HoursPerDay);
  13.   WriteLn('Please enter the number of days you worked this month:');
  14.   ReadLn(DaysPerMonth);
  15.  
  16.   TotalHours := DaysPerMonth * HoursPerDay;
  17.   Salary := TotalHours * DollarsPerHour;
  18.  
  19.   WriteLn('The number of hours you work in a month is: ', TotalHours);
  20.   WriteLn('Your salary this month is: $', Salary);
  21.   ReadLn
  22. end.                                        

The output looks like this:

Quote
Please enter the number of hours you work in a day:
6
Please enter the number of days you worked this month:
23
The number of hours you work in a month is: 138
Your salary this month is: $ 2.1390000000000000E+003

Is there a way to specify the number of decimal places for the final Salary value?

Also, see the third to last line and its ouput - there is a space between the dollar sign and the Salary value; is the a way to concatenate two values of a different type without the space and, preferably, without type-conversion?

Thanks!
« Last Edit: March 23, 2020, 10:09:04 pm by JamesSci »

Handoko

  • Hero Member
  • *****
  • Posts: 5416
  • My goal: build my own game engine using Lazarus

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Code: Pascal  [Select][+][-]
  1. WriteLn('Your salary this month is: $', Salary:0:2);
The ":2" indicates the number of decimals you want to show (for real or float-point numbers)

(":0" is used for right-alignment. You don't need it in this case that's why it's zero)

https://www.freepascal.org/docs-html/rtl/system/write.html
Quote
The format of the numerical conversions can be influenced through the following modifiers: OutputVariable: NumChars [: Decimals ]

This will print the value of OutputVariable with a minimum of NumChars characters, from which Decimals are reserved for the decimals. If the number cannot be represented with NumChars characters, NumChars will be increased, until the representation fits.

If the representation requires less than NumChars characters then the output is filled up with spaces, to the left of the generated string, thus resulting in a right-aligned representation.

If no formatting is specified, then the number is written using its natural length, with nothing in front of it if it's positive, and a minus sign if it's negative.

Real numbers are, by default, written in scientific notation.
« Last Edit: March 23, 2020, 08:09:00 pm by skalogryz »

JamesSci

  • New Member
  • *
  • Posts: 30
Code: Pascal  [Select][+][-]
  1. WriteLn('Your salary this month is: $', Salary:0:2);
The ":2" indicates the number of decimals you want to show (for real or float-point numbers)

(":0" is used for right-alignment. You don't need it in this case that's why it's zero)
Thank you skalogryz, that's very useful.

 

TinyPortal © 2005-2018