Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: floating point percision Date: Mon, 22 Nov 2010 11:46:04 +0000 (UTC) Organization: Element Energy Lines: 43 Message-ID: <icdl5s$7tb$1@fred.mathworks.com> References: <ic6sal$78f$1@fred.mathworks.com> <ic6th4$6tl$1@canopus.cc.umanitoba.ca> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-05-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1290426364 8107 172.30.248.35 (22 Nov 2010 11:46:04 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Mon, 22 Nov 2010 11:46:04 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2310841 Xref: news.mathworks.com comp.soft-sys.matlab:688912 I suppose you can convert to string with desired number of digits and convert back to number. example: b = sprintf('%.2f', 12345.6785) % I ask for 2 digits here b = 12345.68 >> str2num(b) ans = 1.234568000000000e+004 Walter Roberson <roberson@hushmail.com> wrote in message <ic6th4$6tl$1@canopus.cc.umanitoba.ca>... > On 10-11-19 04:05 PM, Eman wrote: > > > how can I determine the number of digits after the decimal point? > > I mean: > > If I need 2 digits after decimal point, If a=74.67859 it should be a=74.68 > > If I need 3 digits after decimal point, If a=74.67859 it should be a=74.679 > > note: I need to use the new value not only format them in printing. > > The only way to do that is to use symbolic numbers with the symbolic toolkit, > or fixed-point numbers with the fixed-point toolbox. > > > You can use transformations such as > > round(a .* 10^D) ./ 10^D > > where D is the number of digits you want after the decimal place. This will > not, however, round to *exactly* 74.68: it would instead round to > 74.68000000000000682121026329696178436279296875 > > When you use binary floating point numbers, the only numbers with two decimal > places that can be *exactly* represented are those that end in .0, .25, .5, or > .75 . It is not possible, for example, to exactly represent 0.1 in binary > floating point: the closest you can get would be > 0.1000000000000000055511151231257827021181583404541015625