How to round decimal number to 5 places?

I am working on Coordinate system based on GPS and have got latlong points to 7digit place ..when i convert them to radian, they expand upto 9 place. My code requires only use upto 5 digits for the calculations. I tried using "round" commands but it says too many input arguments. Can anyone help me with this please.

답변 (2개)

Star Strider
Star Strider 2015년 3월 23일
편집: Star Strider 2015년 3월 23일

1 개 추천

It is easy to create your own version in an anonymous function:
roundn = @(x,n) round(x.*10.^n)./10.^n;
pi_5 = roundn(pi,5)
produces:
pi_5 =
3.14159
If you know you always want to round the radian angle to 5 decimal places, change the function to:
round5 = @(x) round(x.*10.^5)./10.^5;
Shantanu Jana
Shantanu Jana 2015년 3월 23일
편집: Shantanu Jana 2015년 3월 23일

0 개 추천

you can do like this
>> a=1.12345678
a =
1.123456780000000
>> sprintf('%0.5f', a)
ans =
1.12346
>>

댓글 수: 3

Star Strider
Star Strider 2015년 3월 23일
The only problem with that approach is that the output is a string, not a double-precision number.
Shantanu Jana
Shantanu Jana 2015년 3월 23일
X = str2double(ans) it can solve the problem
Michael Stumpf
Michael Stumpf 2020년 5월 1일
I just tried using str2double(ans) , but it ends up displaying all the zeros at the end again

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2015년 3월 23일

댓글:

2020년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by