3

Example: 6.321: I need it to be 6.322. 5.14875: I need it to be 5.14876.

How can I do this?

  • 2
    That might be harder than you think (EDIT: much easier than *I* think, see coment below by @thewaywewalk): [6.321 is usually not 6.321](http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab/686454#686454). If you can clearly define in mathematical terms how the *last* decimal should be selected, then it's easy (multiply by a sutiable power of 10, round, divide again by the same power to get original magnitude). – Andras Deak -- Слава Україні Feb 08 '16 at 20:21
  • 2
    @AndrasDeak you are aware that `round` has multiple input arguments nowadays? – Robert Seifert Feb 08 '16 at 20:23
  • 1
    @thewaywewalk obviously not, thanks:) In my defense, my R2012b has a single argument (but I wouldn't have known if it had more, so your point is completely valid). Also, will it figure it out for you how much you want to round? Or did you only mean that for the last part of my comment? – Andras Deak -- Слава Україні Feb 08 '16 at 20:26
  • For example, The number 5645.132457 I need to add 1 to the 7 so the number could be: 5645.132458 – Daniel Alzate Feb 08 '16 at 20:26
  • Except OP doesn't seem to want to round, rather, he wants to add 1 to the least significant non-zero digit in the decimal representation. If I got the example right ... – zeeMonkeez Feb 08 '16 at 20:27
  • For example, The number 5645.132457 I need to add 1 to the 7 so the number could be: 5645.132458 – Daniel Alzate Feb 08 '16 at 20:29
  • @DanielAlzate the real question is: what about 5645.1324569998 then? – Andras Deak -- Слава Україні Feb 08 '16 at 20:31
  • I don't think this is actually trivial. Have a look at this [MATLAB Answers post](http://www.mathworks.com/matlabcentral/answers/142819-how-to-find-number-of-significant-figures-in-a-decimal-number) to find significant digits – zeeMonkeez Feb 08 '16 at 20:31
  • @AndrasDeak i guess it would be 5645.1324569999 XD – Daniel Alzate Feb 08 '16 at 20:32
  • @DanielAlzate now that's a problem, see my first comment. – Andras Deak -- Слава Україні Feb 08 '16 at 20:32
  • Another question, how could i count the number of decimals a number has. For example, 2.1546 has 4 decimals, 345.12 has 2. – Daniel Alzate Feb 08 '16 at 20:37
  • Do you get numeric data from MATLAB (i.e. doubles with floating point precision errors) or do you load your data from file? – Adriaan Feb 08 '16 at 20:37
  • 1
    @Adriaan I use input – Daniel Alzate Feb 08 '16 at 20:39
  • @zeeMonkeez I rejected your edit because it's especially bad to put the language up front in a title. Most people don't even want to see anything that can be conveyed in tags in the title; I accept that the language might be informative, but exclusively at the end of the title. If you edit it to be in front, you'll almost always be rejected/rolled back. [See here](http://meta.stackexchange.com/a/130208/313143). – Andras Deak -- Слава Україні Feb 08 '16 at 20:44
  • 1
    @AndrasDeak I see. Makes sense. Most importantly, the meaning is fixed :) – zeeMonkeez Feb 08 '16 at 20:45

3 Answers3

4

If you represent numbers as floating point or double precision floating point, this problem is a disaster.

If you can read in a number as a string (you mentioned get the number with the input command), you could do:

x = input('ENTER A NUMBER: ','s');
decimal_place = find(fliplr(x)=='.',1) - 1;

x_val = str2double(x);
if(~isempty(decimal_place))     
    y = x_val + 10 ^ -decimal_place;
else % if there is no decimal place, find first non-zero digit to get sigfig
    warning('ambiguous number of significant digits');
    first_nonzero_digit = find(fliplr(x)~='0',1);
    if(~isempty(first_nonzero_digit))
      y = x_val + 10 ^ (first_nonzero_digit - 1);
    else
      y = x_val + 1;
    end
end

disp('your new number is');
disp(y);

Example test runs:

ENTER A NUMBER: 1.9
your new number is
     2
ENTER A NUMBER: 3510
your new number is
     3520
ENTER A NUMBER: 323.4374
your number is
  323.4375
ENTER A NUMBER: 0
your number is
     1
Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30
  • @Oleg if there are 3 significant digits, and he wants to increment the last significant digit by one, then 3510 becomes 3520. If that's not what he wants, he can trivially remove that code. If there's no decimal point, there can be ambiguity in the number of significant digits. See https://www.physics.uoguelph.ca/tutorials/sig_fig/SIG_dig.htm – Matthew Gunn Feb 08 '16 at 23:03
  • Thanks for the link, I suspected I always got the meaning of significant digits wrong. – Oleg Feb 09 '16 at 01:00
1

@AndrasDeak - I think you're right the first time. The hard part is not the rounding - it's defining the "last" decimal.

Since floating point numbers aren't exact, I can't think of a reliable way to find that "last" decimal place - in any language.

There is a very hacky way that comes to mind, though. You could "print" the number to a string, with 31 numbers after the decimal point, then working right from the dot, find the first place with 15 0s. (Since double precision numbers can only stably represent the first 14 decimal places and you get a 15th that varies, 31 decimal place will ALWAYS give you at least 15 0s after the last sig digit.)

>> a = 1.34568700030041234556

a =

    1.3457

>> str = sprintf('%1.31', a)

str =

   Empty string: 1-by-0


>> str = sprintf('%1.31f', a)

str =

1.3456870003004124000000000000000

>> idx = strfind(str, '000000000000000')

idx =

    19

>> b = a*10^(idx(1)-3)

b =

   1.3457e+16

>> sprintf('%10.20f', b)

ans =

13456870003004124.00000000000000000000

>> c = b+1

c =

   1.3457e+16

>> sprintf('%10.20f', c)

ans =

13456870003004124.00000000000000000000

>> final = floor(c)/10^(idx(1)-3)

final =

    1.3457

>> sprintf('%10.31f', final)

ans =

1.3456870003004124000000000000000

I think that's a relatively reliable implementation.

http://www.mathworks.com/matlabcentral/answers/142819-how-to-find-number-of-significant-figures-in-a-decimal-number

Marc
  • 3,259
  • 4
  • 30
  • 41
-4

assuming your just trying to do regular rounding:

i'd use the round function built into matlab.

let's do your example above..
5.14875 has 5 decimal places and you want it to be converterd to 5.14876.

Lets assume you that the 6th decimal place was 9 (so your number is 5.148759)

%Step 1:changethe format so that your going to be able to see all of the
%decimal places

format long 
%step2:now enter the original number
OriginalNumber=5.148755

%step 3 take the original number and round it to your new number
NewNumber=round(OriginalNumber,5)

this solution will not work if the 6th number (that you did not show) was a <5 because the computer will not know to round up

assuming your just trying to cut the numbers off...

You cannot do this in regular default matlab floating point numbers. To keep my explination simple I'll just state that without an explination. I'd do some review on the different ways matlab stores # (int vs floating point) on the matlab website. They have excellent documentation.

Community
  • 1
  • 1
LeeRuns
  • 434
  • 4
  • 14