# How to cut digits after the decimal point?

380 views (last 30 days)
Mark Golberg on 1 Mar 2017
Commented: Deepthi B on 21 Apr 2021
Hello, I have the following variable:
a1 = 0.1234
I want to cut all digits after the first one after the decimal point. Meaning:
a2 = 0.1
I've created the following code (q=1 in our example):
b1 = a1 .* 10^q;
b2 = floor(b1);
a2 = b2 ./ (10^q);
But, it output
a2 = 0.1000
How can I get rid of the zeros in the end?
Thank you.

John D'Errico on 1 Mar 2017
Edited: John D'Errico on 1 Mar 2017
You can use
a2 = round(a1,1);
to round to that decimal point.
However, to display the number with no zeros, you may want to change your display format.
format short
a2
a2 =
0.1000
format short g
a2
a2 =
0.1
See the difference between short, and short g.
I tend to leave the display format as short g always, except for the rare time when I want to see more digits, then I move to long g.
##### 2 CommentsShowHide 1 older comment
Deepthi B on 21 Apr 2021
Can we save this variable in this format?

Edited: Adam on 1 Mar 2017
a2 = round( a1, 1 );
if you have a sufficiently recent version of Matlab. I can't remember when this functionality was added to the round function though.