フィルターのクリア

how to get the value a number at a specify decimal position ?

1 回表示 (過去 30 日間)
Paul Nanfah
Paul Nanfah 2015 年 9 月 17 日
コメント済み: Paul Nanfah 2015 年 9 月 18 日
hi, If I have
pi=3.1415926535897932384626433832795028
I want to get the value of lets say 10th number after the comma.
n = 5;
it this possible? I though I could convert the number to a vector
and then proced with index but i dont know how to get a vector out the given decimal number. thanks
  1 件のコメント
Image Analyst
Image Analyst 2015 年 9 月 17 日
Sounds like homework. Is it? If so, add a homework tag below.

サインインしてコメントする。

採用された回答

James Tursa
James Tursa 2015 年 9 月 17 日
編集済み: James Tursa 2015 年 9 月 17 日
Use sprintf, find the decimal point, then get the desired digit after the decimal point. That being said, realize that for some numbers there may be rounding involved in the string output so if you push this technique to the limit it can become a much more difficult problem.
  2 件のコメント
Image Analyst
Image Analyst 2015 年 9 月 17 日
place = 10 % Decimal place you want to extract.
myNumber = pi % Whatever your number is.
% Convert fractional part to string.
str = num2str(rem(myNumber, fix(myNumber)))
% Extract the digit.
n = str(place+2) - '0'
but this doesn't work if the place is too big, like 10 or 100 or something.
Paul Nanfah
Paul Nanfah 2015 年 9 月 18 日
thx a lot !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by