forums.silverfrost.com
Welcome to the Silverfrost forums

Author Message
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Sat Mar 25, 2023 12:18 am    Post subject: Format question What would be the format string for a real, maximum positions before the decimal point: thirteen and two positions after the decimal point? What I am really after is displaying a financial number with € in front. I have looked at the tutorials with no avail. Thank you, Zach
mecej4

Joined: 31 Oct 2006
Posts: 1840

Posted: Sat Mar 25, 2023 2:40 am    Post subject:

You could use F17.2; if you know that the numbers will always be positive, F16.2.

 Code: program money implicit none double precision budget budget = 1234567890123.12d0 print 10,budget,-budget 10 format(2(1x,'€ ',F17.2)) end

Depending on the code page in your development environment, the Euro symbol may not be rendered correctly.
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Sat Mar 25, 2023 9:12 am    Post subject: Re your code, for which I thank you, in a financial application I wouldn't end up with values that have d0 at the end. Could you propose an example without that d0 at the end? Indeed the numbers could be negative.
mecej4

Joined: 31 Oct 2006
Posts: 1840

 Posted: Sat Mar 25, 2023 9:25 am    Post subject: If you have a literal real in a program and you leave out the "d0", that number would be chopped/rounded to the precision of a 32-bit real, i.e., about seven significant decimal digits. You specified that you wanted to process numbers with 13+2 digits; for that, you need double precision. How to write the code is dependent on where your numbers come from. A calculation, or from reading a data file? If the former, your variables being printed out would have to be double precision. If the latter, you have to choose an appropriate input format. Please read the formatted I/O chapter of a Fortran book to learn the details.
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Sat Mar 25, 2023 9:42 am    Post subject: Yes I need a book. You are right. Learning from the Net has its limitations. I already have one on order. I don't have more than two decimals output in a financial program. So how many positions before the decimal point would I be limited to, if I use a real in stead of a standard precision?
mecej4

Joined: 31 Oct 2006
Posts: 1840

 Posted: Sat Mar 25, 2023 10:07 am    Post subject: There is a major problem with what you are attempting to do, if I am not mistaking what that is. You want (I think) to do fixed point real arithmetic with fifteen significant digits. Fortran and the hardware of modern PCs are set up to do floating point real arithmetic, instead, with 24 bit or 53 bit precision. See https://en.wikipedia.org/wiki/Fixed-point_arithmetic .
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Sat Mar 25, 2023 2:45 pm    Post subject: Thank you for your help. I have ordered a textbook and am putting everything on hold until I have done some necessary studying.
mecej4

Joined: 31 Oct 2006
Posts: 1840

 Posted: Sat Mar 25, 2023 5:04 pm    Post subject: Support for decimal arithmetic in currently popular programming languages and hardware support on microcomputers is not sufficient for financial applications and decimal arithmetic. An expert's review of the issues is in this paper by Mike Cowlishaw, "Decimal Floating-Point: Algorism for Computers": https://typeset.io/papers/decimal-floating-point-algorism-for-computers-37ixu8zvte
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1