You cannot. There is no exact representation in floating point
for .01, .02, up to .99, except for 0.25, 0.50, and 0.75 which
have exact representations. If you were to try to round
(say) pi to 2 decimal places then the value stored internally
would not correspond to 3.14 but instead to approximately
3.14 + 2e-15
If you are only attempting to change what is displayed when
you specifically ask to output something, then you can
format the numbers using num2str or sprintf() using a format
such as '%0.2f' .
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
x = round((x*100))/100
x = round((x*100))/100
Hi,
>> x=pi;
x = round((x*100))/100
sprintf('%.16f',x)
x =
3.1400
ans =
3.1400000000000001
Jerome (Dut)
Small correction: the result is
3.14 + 1.24344978758017532527446746826171875e-16
>> sprintf('%0.60g',3.14)
ans =
3.140000000000000124344978758017532527446746826171875
--
"All human knowledge takes the form of interpretation."
-- Walter Benjamin
here is one way:
data=round(data*100)/100
rounding functions in matlab:
round
fix
floor
ceil
look in the help for more information.
>here is one way:
>
>data=round(data*100)/100
>> data=pi; data=round(data*100)/100; sprintf('%.60g', data)
ans =
3.140000000000000124344978758017532527446746826171875
Why didn't I get 3.14 exactly? I followed your steps word for word!
Is sprintf broken???
Hi Benjamin,
no sprintf is fine the problem occurs by /100. as 100 is no
power of 2 rounding errors occur
this will always occur when you divide.
for an exact rounding try
str2num(sprintf('%5.2f',pi))
which looks strange to me but seems to work
kinor
> for an exact rounding try
> str2num(sprintf('%5.2f',pi))
> which looks strange to me but seems to work...
does it?
format long;
r=str2num(sprintf('%5.2f',pi))
% r =
% 3.140000000000000
% however,
s=sprintf('%.20f\n',r,3.14)
% s =
% 3.14000000000000010000 % <- result
% 3.14000000000000010000 % <- hand coded
us
>> >here is one way:
>> >data=round(data*100)/100
>> >> data=pi; data=round(data*100)/100; sprintf('%.60g', data)
>> ans =
>> 3.140000000000000124344978758017532527446746826171875
>> Why didn't I get 3.14 exactly? I followed your steps word
>for word!
>> Is sprintf broken???
>no sprintf is fine the problem occurs by /100. as 100 is no
>power of 2 rounding errors occur
But Dan said that his way worked! Are you saying that he was WRONG?
>this will always occur when you divide.
>> data=3.25;data=round(data*100)/100; sprintf('%.60g', data)
ans =
3.25
Where did the round-off error in Dan's routine go? I can't find it?
Do I need a wider format than %.60g to see it??
>for an exact rounding try
>str2num(sprintf('%5.2f',pi))
>which looks strange to me but seems to work
>> data=str2num(sprintf('%5.2f',pi)); sprintf('%.60g', data)
ans =
3.140000000000000124344978758017532527446746826171875
Why didn't I get 3.14 exactly? I followed your steps word for
word! Is sprintf broken???
--
"Do diddle di do,
Poor Jim Jay
Got stuck fast
In Yesterday." -- Walter De La Mare
where the specifier '%g' avoids showing the last
or 'insignificant' zeros and shows only the two decimals
product of multiplying by 100.
>> sprintf('%g',round(pi*100)/100)
ans =
3.14
"carlos lopez" <clv2clv_...@adinet.com.uy> wrote in
message <fqnaea$370$1...@fred.mathworks.com>...
Hello,
Try this
x = 1.23456
quant(x,2)
function output = quant(number,digit)
output = round(number.*(10 ^ digit))./(10 ^ digit);
--Zia
> Try this
> x =3D 1.23456
> quant(x,2)
you should tell the OP that your solution requires the
neural network tbx...
us
humm.. he pasted the one line source code for quant().
"function output = quant(number,digit)
output = round(number.*(10 ^ digit))./(10 ^ digit);"
So one can just copy it and use it. no need for neural netwrok toolbox?
Nasser