How to reduce precision of digit after decimal point? Matlab (R2020a)

조회 수: 4 (최근 30일)
taimour sadiq
taimour sadiq 2020년 10월 23일
댓글: Ameer Hamza 2020년 12월 10일
if i have
a = 1.2345 or
a = 22.34325 or
a = 334.659809832
i want to reduce precision after decimal point to two digit so results should be
a= 1.23
a = 22.34
a = 334.65
i have tried ceil,floor, vpa, round(a,2) but this doesnt give me the above required result... i have searched the matlab answer mostly old questions replied with "fprintf which may support older versions. Kidly Guide

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 23일
편집: Ameer Hamza 2020년 10월 23일
If you are only concerned with displaying the results with two digits after the decimal, then you can use fprintf
a = 334.659809832
fprintf('a=%.2f\n', a)
  댓글 수: 13
taimour sadiq
taimour sadiq 2020년 12월 9일
I m Very Gratefull to You Ameer i was struggling to do this for many weeks....Finally with your Guidence i have achieved the desired Goal... This is what i wanted...More than Thanks for ur Help and Support...
Ameer Hamza
Ameer Hamza 2020년 12월 10일
I am glad to be of help! :)

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

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2020년 10월 23일
hi
dirty trick , example to round to 2 digits after decimal :
aa = 0.01*round(a*100)
example :
0.01*round(pi*100)
ans =
3.1400
or when you convert to string :
disp(num2str(pi,3))
3.14

카테고리

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