How can i use round function to change decimal value to integer ?

조회 수: 1 (최근 30일)
Maria
Maria 2022년 9월 15일
댓글: Maria 2022년 9월 15일
Hi all!
i used the function "round" in order to round values in matrix, but it does not display the required result.
for example i have
x=1.5919 1.0e+06 *
i used
A=round(x)/(10^6)
i get this
X=1.5919 ,but the result i want is X=2.
Thanks in advance

채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 15일
format long g
x = 1.5919 * 1.0e+06
x =
1591900
round(x/10^6)
ans =
2
If you are wanting to round to 1 significant digit, then it gets a bit more complicated in vectorized form
x = [pi*10^6, exp(1)*10^-5, -sqrt(2)*10^2, -inf, inf, 0, nan].'
x = 7×1
1.0e+00 * 3141592.65358979 2.71828182845904e-05 -141.42135623731 -Inf Inf 0 NaN
scale = 10.^floor(log10(abs(x)));
scale(scale == 0 | ~isfinite(scale)) = 1;
A = round(x ./ scale) .* scale
A = 7×1
1.0e+00 * 3000000 3e-05 -100 -Inf Inf 0 NaN

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by