How do I drop decimal places without rounding?

30 ビュー (過去 30 日間)
Joseph Mayer
Joseph Mayer 2016 年 1 月 26 日
コメント済み: Walter Roberson 2020 年 11 月 6 日
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.

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 1 月 26 日
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 件のコメント
Joseph Mayer
Joseph Mayer 2016 年 1 月 26 日
Thanks for the answer. Apparently there is a 'chop' function that does this way simpler though!
Walter Roberson
Walter Roberson 2016 年 1 月 27 日
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.

サインインしてコメントする。


Star Strider
Star Strider 2016 年 1 月 26 日
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 件のコメント
Walter Roberson
Walter Roberson 2016 年 1 月 27 日
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.

サインインしてコメントする。


Denman James
Denman James 2020 年 11 月 6 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 11 月 6 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by