how convert fraction into integer?

조회 수: 4 (최근 30일)
huda nawaf
huda nawaf 2012년 1월 11일
hi, how convert fraction part into integer :
30.6789 to 6789
thanks
  댓글 수: 2
huda nawaf
huda nawaf 2012년 1월 11일
i want add something
when i get 6789 for ex.
i want change it into 6790 for ex.
and return this no. into fraction such as: 30.6790
thanks
Walter Roberson
Walter Roberson 2012년 1월 11일
Why not just add .0001 ?

댓글을 달려면 로그인하십시오.

답변 (2개)

Jonathan Sullivan
Jonathan Sullivan 2012년 1월 11일
Looks like you want to round to the nearest 0.001. You can do this with the following:
x = 30.6789 % Or whatever your data is
base = 0.001; % Or whatever you are rounding too.
x_rounded = round(x./base).*base;
Hope this helps!
  댓글 수: 2
huda nawaf
huda nawaf 2012년 1월 11일
thanks,
but i need isolate the integer from fraction , then i can change the fraction as i need.
i need get the fraction part whatever it, i do not know it and do not know it's lengths.
i need the fraction part alone , then may hide in it bits then return it as fraction after modify it
Walter Roberson
Walter Roberson 2012년 1월 11일
The fraction you are looking for would have to be in base 10, but in base 10 it would be inappropriate to work at the *bit* level; instead you would have to work at the *dit* level (though the proper name for a "dit" is a "ban"; http://en.wikipedia.org/wiki/Ban_%28information%29)
Mixing base 10 and base 2 needs to be done very carefully!

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 1월 11일
How about this:
a=30.6789;
out = (a - floor(a)) * 10000
You do need the base, for example 10000. Why? Because the fraction never ends as I'm sure you know because you've read the FAQ. Want proof? Check this out:
format long;
a % Display it. It won't be 30.6789!
>> a =
30.678899999999999
So that's why you have to pick some number of decimal places.
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 1월 11일
For the benefit of those who might not have read the referenced FAQ,
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by