write a function to round off numbers
5 ビュー (過去 30 日間)
古いコメントを表示
Write your own roundn function that takes a floating point value as its input and rounds its nth decimal digit to the nearest number.
>> res = roundn(5.672,3); % results in res = 5.67
>> res = roundn(5.677,3); % results in res = 5.68
>> res = roundn(-5.672,3); % results in res = -5.67
>> res = roundn(-5.677,3); % results in res = -5.68
0 件のコメント
回答 (2 件)
Image Analyst
2014 年 2 月 28 日
Try multiplying by a factor of 10 then rounding and dividing. That should be a good hint - probably too much of one.
2 件のコメント
Image Analyst
2014 年 2 月 28 日
Instead of checking for every possible factor of 10, why don't you just use 10^myFloat?
And how are you running it, if not from the command line? What value are you passing in when it does not run?
Walter Roberson
2014 年 2 月 28 日
This cannot be done in MATLAB, not if the values are to be stored numerically as datatype single() or double().
>> fprintf('%.999g\n', 0.1)
0.1000000000000000055511151231257827021181583404541015625
This establishes that even if you had 0.1 already given, that you do not actually get exactly 0.1 stored.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!