- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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!
링크가 복사됨
5 응답
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.)
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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