How do I drop decimal places without rounding?

27 visualizaciones (últimos 30 días)
Joseph Mayer
Joseph Mayer el 26 de En. de 2016
Comentada: Walter Roberson el 6 de Nov. de 2020
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.

Respuestas (3)

Walter Roberson
Walter Roberson el 26 de En. de 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 comentarios
Joseph Mayer
Joseph Mayer el 26 de En. de 2016
Thanks for the answer. Apparently there is a 'chop' function that does this way simpler though!
Walter Roberson
Walter Roberson el 27 de En. de 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.

Iniciar sesión para comentar.


Star Strider
Star Strider el 26 de En. de 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 comentario
Walter Roberson
Walter Roberson el 27 de En. de 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.

Iniciar sesión para comentar.


Denman James
Denman James el 6 de Nov. de 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 comentario
Walter Roberson
Walter Roberson el 6 de Nov. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by