forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Format question

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Zach



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

PostPosted: Sat Mar 25, 2023 12:18 am    Post subject: Format question Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sat Mar 25, 2023 2:40 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Zach



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

PostPosted: Sat Mar 25, 2023 9:12 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sat Mar 25, 2023 9:25 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Zach



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

PostPosted: Sat Mar 25, 2023 9:42 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Send e-mail
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sat Mar 25, 2023 10:07 am    Post subject: Reply with quote

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 .
Back to top
View user's profile Send private message
Zach



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

PostPosted: Sat Mar 25, 2023 2:45 pm    Post subject: Reply with quote

Thank you for your help. I have ordered a textbook and am putting everything on hold until I have done some necessary studying.
Back to top
View user's profile Send private message Send e-mail
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sat Mar 25, 2023 5:04 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group