modifying/​rounding/a​proximatin​g a number

i need a way to modify a number a=2.65987 into 2.6598 i tried round /fprintf , but both display as 2.6599 ,i need it badly ,i could just pop it out of nowhere by substracting 0.00007 but that's not helping .

 채택된 답변

Star Strider
Star Strider 2020년 10월 30일

1 개 추천

One option:
c = floor(a*1E4)/1E4
producing:
c =
2.6598
.

댓글 수: 6

Opariuc Andrei
Opariuc Andrei 2020년 10월 30일
Thank you . Again :) . Can i also get some documentation regarding the method ?
Star Strider
Star Strider 2020년 10월 30일
As always, my pleasure!
The floor function rounds to negative infinity, although since the number is positive, fix wouild likely have worked as well here, since it rounds toward 0. (The function you choose largely depends on what you want to do, ceil being the other option in general, although that would not work here.)
Since you want to round down to 4 decimal digits to the right of the decimal separator, this works by first multiplying by then calling floor, and dividing the result by . It is possible to round down (or up) to any number of decimal digits by doing the appropriate multiplication, calling the appropriate function, then doing the identical division.
Opariuc Andrei
Opariuc Andrei 2020년 10월 30일
interesting ,very , must be deepened ,something to ask my teacher about (in the hope she knows ) . can i have one more example ? from 0.0000300 to 0.0000299
Star Strider
Star Strider 2020년 10월 30일
It is likely not possible to go from 0.0000300 to 0.0000299 in the absence of floatiing-point approximation error, or otherwise subtracting eps or some other very small number from the original number. See the documentation section on Floating-Point Numbers for an extensive discussion of them.
I am certain she will know!
Opariuc Andrei
Opariuc Andrei 2020년 10월 31일
i checked , she doesn't know , i gave her something to ponder about :) / homework
Star Strider
Star Strider 2020년 10월 31일
Interesting!
Check the link I posted — the link information is correct.
While I would normally suggest that the difference between the two in the precision of the diaplayed numbers could account for the difference, that is not the situation here, where the display precision is not changing.

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2020a

태그

질문:

2020년 10월 30일

댓글:

2020년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by