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 .

 采纳的回答

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

6 个评论

Thank you . Again :) . Can i also get some documentation regarding the method ?
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.
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
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!
i checked , she doesn't know , i gave her something to ponder about :) / homework
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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by