Your answer is one click away!

Daniel Alzate February 2016
### Add 1 to the least significant digit of a number in MATLAB

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

How can I do this?

LeeRuns February 2016
## assuming your just trying to do *regular rounding*:

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

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**

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.

Matthew Gunn February 2016

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
```

Marc February 2016

@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.

Asked in February 2016

Viewed 1,418 times

Voted 11

Answered 3 times

Viewed 1,418 times

Voted 11

Answered 3 times