how to convert 1.228 into 1

1 回表示 (過去 30 日間)
vipul utsav
vipul utsav 2013 年 1 月 23 日
how to convert 1.228 into 1.2,1.366 into 1.7, 1.99999 into 2.0 (i.e. into a .1f )

回答 (2 件)

Andreas Goser
Andreas Goser 2013 年 1 月 23 日
There are a couple of operations you want to try. In this case you are probably looking for SPRINTF. The rest of the "family" is ROUND, FLOOR, CEIL, FIX

Walter Roberson
Walter Roberson 2013 年 1 月 23 日
Is this for display purposes, or for computation purposes?
If it is for computation purposes, then you cannot do that unless you are using symbolic numbers or the fixed-point toolbox, as floating point numbers are represented in binary rather than in decimal.
If it is for display purposes, convert them with sprintf() with a '%.1f' format. If you have an array of them, then sometimes it is easier to
num2str(YourArray, '%.1f ') (notice the space after the f)
  2 件のコメント
vipul utsav
vipul utsav 2013 年 1 月 23 日
it is for computational purpose, if a=[1.233 5.567 3.44] and its size is (1,3)
i want b=[1.2 5.6 3.4] and size is same as a.
Walter Roberson
Walter Roberson 2013 年 1 月 23 日
That is not possible. 0.1, 0.2, 0.3., 0.4, 0.6, 0.7, 0.8, and 0.9 do not have exact representations in binary floating point numbers (but 0.0 and 0.5 do). 0.1 decimal becomes an infinite repeating fraction in binary, just the same way that there is no exact decimal representation for 1/3 or 1/7.
You can round to close to the numbers, but not exactly those numbers.
b_approx = round(a * 10) / 10;

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by