how to round the decimal point?

Hai,
I need to round the decimal number to 2 decimal places. What is the function in matlab, to the above rounding off. Looking for your reply.
BSD

Respuestas (3)

Jan
Jan el 27 de Sept. de 2011

2 votos

x = rand * 1000;
y = floor(x * 100) / 100;
See: floor, ceil, round, fix.
A general method to find help is searching in the documentation:
docsearch round

4 comentarios

bsd
bsd el 27 de Sept. de 2011
floor() rounds the decimal value to an integer. I should not round it to an integer, I wan't the decimal value to be rounded upto 2 decimal place. For example, 0.1698 must be rounded to 0.17. Looking for your reply.
BSD
Jan
Jan el 27 de Sept. de 2011
In you original question you ask for "rounding off". If you want to round to the nearest: round(0.1698 * 100) / 100 replies 0.17
Walter Roberson
Walter Roberson el 27 de Sept. de 2011
>> sprintf('%.99g\n',round(0.1698 * 100) / 100)
ans =
0.1700000000000000122124532708767219446599483489990234375
>> num2hex(round(0.1698 * 100) / 100 )
ans =
3fc5c28f5c28f5c3
>> num2hex(round(0.1698 * 100) / 100 - 8*eps(.01))
ans =
3fc5c28f5c28f5c2
If you compare the hex of the two floating point values, you will note that this second value is the first representable value smaller than the original value
>> sprintf('%.99g\n',round(0.1698 * 100) / 100 - 8*eps(0.01))
ans =
0.169999999999999984456877655247808434069156646728515625
and you can see from the extended printout that it is below 0.17 whereas the other value was above 0.17. We have thus experimentally demonstrated that there is no exact representation in binary floating point arithmetic for 0.17 exactly.
Jan
Jan el 28 de Sept. de 2011
@BSD: Does Walter's correct argument concern your work? Or is 0.1700000000000000xyz... accurate enough for your demands?

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 27 de Sept. de 2011

0 votos

This is not possible in binary floating point arithmetic, unless the rounded fraction happens to end up being 0.0, 0.25, 0.5, or 0.75
Binary floating point arithmetic cannot exactly represent the fraction 1/100 in any finite amount of storage, for the same reason that decimal arithmetic cannot exactly represent the fraction 1/7 in any finite amount of storage.
Jigar Gada
Jigar Gada el 28 de Ag. de 2012

0 votos

Thanks a lot.. It was so simple.

Categorías

Más información sobre Discrete Data Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

bsd
el 27 de Sept. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by