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 .

 Akzeptierte Antwort

Star Strider
Star Strider am 30 Okt. 2020

1 Stimme

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

6 Kommentare

Opariuc Andrei
Opariuc Andrei am 30 Okt. 2020
Thank you . Again :) . Can i also get some documentation regarding the method ?
Star Strider
Star Strider am 30 Okt. 2020
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 am 30 Okt. 2020
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 am 30 Okt. 2020
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 am 31 Okt. 2020
i checked , she doesn't know , i gave her something to ponder about :) / homework
Star Strider
Star Strider am 31 Okt. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by