How do I drop decimal places without rounding?

118 views (last 30 days)
I have a function that the user inputs a value. I only need 6 decimal places after the decimal point and cannot round.
example - the answer to the function is 1.1234567890123 but I only need to output 1.123456 and just drop the rest of the places without rounding. (no floor / ceil / etc.) I can't seem to find an answer on here.

Answers (3)

Walter Roberson
Walter Roberson on 26 Jan 2016
fix(X * 10^6)/10^6
This accounts for negative values as well. If your values were known to be non-negative then you could also use
floor(X * 10^6)/10^6
Note: in binary floating point arithmetic, it is not possible to exactly represent 0.000001 or multiples of that, except for the values that happen to be negative powers of 2, or an integer multiple of that, such as 0.5, 0.25, 0.375 . The fix() and floor() methods only get you closer to pure decimal number. For example, 1.123456 internally is represented by 1.12345600000000001017497197608463466167449951171875
  2 Comments
Joseph Mayer
Joseph Mayer on 26 Jan 2016
Thanks for the answer. Apparently there is a 'chop' function that does this way simpler though!
Walter Roberson
Walter Roberson on 27 Jan 2016
chop() is one part of a larger File Exchange Contribution, not a built-in routine, and it chops by binary positions not by decimal positions.

Sign in to comment.


Star Strider
Star Strider on 26 Jan 2016
If you can’t use floor, ceil, fix, etc., the only remaining option is to convert to and from string representations:
format long g % Set Default Format
N = 1.1234567890123; % Number
Ns = num2str(N, '%.15f'); % String Representation
dp = regexp(Ns, '[.]'); % Find Decimal Point Position
Out = str2double(Ns(1:dp+6)) % Display Number To Six Places Right Of The Decimal
format short eng % Set Default Format
Out =
1.123456
  1 Comment
Walter Roberson
Walter Roberson on 27 Jan 2016
num2str uses rounding (except when it is implemented on systems with bugs in their C libraries, which has been known to happen.)
>> num2str(1.123456999999999999, '%.15f')
ans =
1.123457000000000
even though the exact internal representation is 1.1234569999999999279083340297802351415157318115234375 which needs to go to 1.123456 for the user's purpose.

Sign in to comment.


Denman James
Denman James on 6 Nov 2020
I ran into a similar problem that I needed to solve relative to the original question while making a tool to explain rounding to my kids. One potential solution is to note that the user wants to truncate the number without rounding in the 1e-6 place. You can use floor to get you there by multiplying the number to get the decimal place where the truncation is to take place, use the floor function to discard the decimals, and then re-divide by 1e6.
a = 1.1234567890123;
result = floor(a*1e6)/1e6;
result = 1.123456000000000
  1 Comment
Walter Roberson
Walter Roberson on 6 Nov 2020
Not quite.
format long g
a = 1.1234567890123;
result_pos = floor(a*1e6)/1e6
result_pos =
1.123456
result_neg = floor(-a*1e6)/1e6
result_neg =
-1.123457

Sign in to comment.

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!