From: Nuri on
Hi,

I have a simple question. The real value of my equation is 3.31831 but I want to use it as 3.31

I tries "round" function but it gives 3, not 3.31

How can I shorten my value? Thanks for your help
From: Matt Fig on
All the usual fp issues not withstanding:

x = 3.31831;
x = floor(x*100)/100
From: sscnekro on
Here's the trick: multiply your number first by appropriate 10, 100, etc., use round and divide back. Alternatively, check out sprintf(). I only know either of these tricks as I was reading posts.
From: dpb on
Nuri wrote:
> Hi,
>
> I have a simple question. The real value of my equation is 3.31831 but I
> want to use it as 3.31
> I tries "round" function but it gives 3, not 3.31
>
> How can I shorten my value? Thanks for your help

Depends on what your definition of "use it" is...

>> x=3.31831;
>> y=round(x*100)/100
y =
3.3200
>> y=str2num(num2str(x,3))
y =
3.3200
>> y = str2num(sprintf('%2.2f',x))
y =
3.3200
>> s=sprintf('%2.3f',x);y = str2num(s(1:findstr(s,'.')+2))
y =
3.3100
>>

Note that all the above except the last round the last digit to "2"
rather than showing the unround "1".

So, what's a solution also depends on whether you mean only for display
purposes or whether you want the internal representation to only have
(as near as can be approximated given the characteristics of floating
point representation) to have some specific number of digits of precision.

--

From: us on
"Nuri " <nuri87(a)gmail.com> wrote in message <hv0tcp$gam$1(a)fred.mathworks.com>...
> Hi,
>
> I have a simple question. The real value of my equation is 3.31831 but I want to use it as 3.31
>
> I tries "round" function but it gives 3, not 3.31
>
> How can I shorten my value? Thanks for your help

one of the (pseudo-) solutions

v=3.31831;
r=round(100*v)/100
% r = 3.32

us