Function that returns value after decimal point?

1 回表示 (過去 30 日間)
sono
sono 2012 年 10 月 4 日
Is there a function that returns the value after the decimal?
EX:
a = 2.876
function(a) = 0.876
Thank You

採用された回答

Jan
Jan 2012 年 10 月 4 日
編集済み: Jan 2012 年 10 月 4 日
a = 2.876
b = mod(a, 1)
What is the wanted output for negative numbers? REM might be helpful:
x = -3.14;
mod(x, 1) % 0.86
rem(x, 1) % -0.14
abs(rem(x)) % 0.14
mod(abs(x), 1) % 0.14
  1 件のコメント
sono
sono 2012 年 10 月 7 日
Thanks. I was using this to calculate the relative 0-2pi angle from one greater then 2pi.
Negative angles were important because it was for quadrant identification and if angle is negative you have to spin around origin in opposite direction so keeping the sign was important. To make the function work the same way for negative angles I just used:
-mod(abs(a),1)
Making it positive so mod(a,1) function would work in right direction then re-adding the negative sign.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
編集済み: Azzi Abdelmalek 2012 年 10 月 4 日
out=a-fix(a)
%or
A=num2str(a);
out=str2num(['0' A(strfind(A,'.'):end)])
  2 件のコメント
Jan
Jan 2012 年 10 月 4 日
What happens for integer values?
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 4 日
編集済み: Azzi Abdelmalek 2012 年 10 月 4 日
a=1
a-fix(a)=0
and
A=num2str(a);
out=str2num(['0' A(strfind(A,'.'):end)])=0

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

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by