- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have to ask a dummy question.
X=7.56, I want to get how many decimal places in X. Here it is 2.
What function should I use?
Thanks!
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm not certain what you mean by this, but it seems unlikely there is a single existing function which would do what you have in mind. If X is a binary floating point value, you could use internal write, probably with G format, to convert it to a character string with the appropriate number of digits (6, including those before the decimal point, for single precision). Then count the non-zero digits after the decimal point.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is important to note that many decimal fractional values cannot be exactly represented in binary floating point. So the approach Tim suggests, which rounds the value to some number of digits, is about the only plausible approach. You have to decide how many extra digits to round to, keeping in mind the fixed precision of the data type (6 or 7 significant digits for single precision, 15 for double.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you can provide sample codes, that would be very helpful.
I am not 100% sure how to do it.
Thanks!
Roy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ok its not often I use recursion and its not Tims method but anyway
program CountDecPlaces
implicit none
! Variables
integer CountDecimal
integer CountDecimal
! Body of CountDecPlaces
write(*,*)CountDecimal(1.0,0)
write(*,*)CountDecimal(1.234,0)
write(*,*)CountDecimal(1.34,0)
write(*,*)CountDecimal(10.0,0)
write(*,*)CountDecimal(12.1,0)
write(*,*)CountDecimal(1000.0001,0)
write(*,*)CountDecimal(1.0,0)
write(*,*)CountDecimal(1.234,0)
write(*,*)CountDecimal(1.34,0)
write(*,*)CountDecimal(10.0,0)
write(*,*)CountDecimal(12.1,0)
write(*,*)CountDecimal(1000.0001,0)
end program CountDecPlaces
recursive integer function CountDecimal(Number,Iterations)
integer iterations
realNumber
realNumber
if (Number .eq. int(number)) then
CountDecimal=Iterations
else
CountDecimal=CountDecimal(Number*10,Iterations+1)
end if
CountDecimal=Iterations
else
CountDecimal=CountDecimal(Number*10,Iterations+1)
end if
end function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I solved it. Thanks!

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page