Rounding a decimal down

조회 수: 4 (최근 30일)
L'O.G.
L'O.G. 2022년 12월 2일
댓글: Emre Tas 2023년 8월 8일
With round(x,2) I can round a number to the nearest hundredth, but how do I round down to the nearest hundredth? For example, both 0.143 and 0.147 should become 0.14.

채택된 답변

Matt J
Matt J 2022년 12월 2일
편집: Matt J 2022년 12월 2일
x=0.147;
floor(x*100)/100
ans = 0.1400
  댓글 수: 1
Emre Tas
Emre Tas 2023년 8월 8일
Thank you for your answer, it solved my meshing problem.

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

추가 답변 (2개)

Les Beckham
Les Beckham 2022년 12월 2일
x = [0.143 0.147];
floor(x*100)/100
ans = 1×2
0.1400 0.1400

Vilém Frynta
Vilém Frynta 2022년 12월 2일
편집: Vilém Frynta 2022년 12월 2일
Example:
X = 0.143;
Y = sprintf('%.2f',X);
Y = str2double(Y)
Y = 0.1400
Someone ahd same question before. Next time, try to Google your question before asking.
  댓글 수: 2
Stephen23
Stephen23 2022년 12월 3일
편집: Stephen23 2022년 12월 3일
It is more efficient to keep this in the numeric domain, rather than converting to text and back.
It also does not work, because SPRINTF rounds values to the nearest digit, it does not round down:
X = 0.147;
Y = sprintf('%.2f',X);
Y = str2double(Y) % Nope, definitely not rounded down.
Y = 0.1500
The answers from Matt J and Les Beckham are correct.
Vilém Frynta
Vilém Frynta 2022년 12월 3일
True, I just reffered to someone else's answer.

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

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by