Hi everyone,
I have two numbers and want to round up and round down them.
minval = 0.4410 and maxval=0.8450
I want to round down the first number and it will be 0.4. The 2nd number after being rounded up will be 0.9.
I tried with floor(minval) it returns 0 and ceil(maxval) it returns 1. Those numbers are not the result I want.
Could someone give me some advice?
I'm using MATLAB R2014a.
Thank you so much.

1 件のコメント

shaadinama
shaadinama 2023 年 5 月 8 日
編集済み: DGM 2023 年 5 月 11 日
In MATLAB, the functions "floor" and "ceil" can be used to round down and round up, respectively.
The "floor" function rounds a given input value down to the nearest integer. For example, "floor(3.7)" would return 3, and "floor(-2.3)" would return -3.
The "ceil" function, on the other hand, rounds a given input value up to the nearest integer. For example, "ceil(3.2)" would return 4, and "ceil(-2.9)" would return -2.
Both functions can be useful in various mathematical and programming contexts where it is necessary to round numbers to the nearest whole integer.

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

 採用された回答

Star Strider
Star Strider 2014 年 12 月 30 日
編集済み: Star Strider 2014 年 12 月 30 日

5 投票

In R2014b, the round function now supports rounding to a specific number of digits:
minval = 0.4410;
maxval = 0.8450;
r1_minval = round(minval, 1);
r1_maxval = round(maxval, 1);
If you don’t have R2014b, this works:
diground = @(x,d) round(x*10^d)/10^d;
r2_minval = diground(minval,1);
r2_maxval = diground(maxval,1);

7 件のコメント

Khanh
Khanh 2014 年 12 月 30 日
Thank you.
Star Strider
Star Strider 2014 年 12 月 30 日
My pleasure!
Jan
Jan 2017 年 7 月 17 日
@NaySan: Please use flags only, to mark inapproriate messages, which should be reviewed by the admins or editors. Thanks.
Pablo López
Pablo López 2018 年 7 月 3 日
Hi!
This code rounds down both values instead of rounding maxval to 0.9. Is it possible to specify what we would like to do? (round up or down)
Thanks!
Star Strider
Star Strider 2018 年 7 月 3 日
Probably. You would have to write your own function.
marchammer
marchammer 2022 年 2 月 25 日
編集済み: marchammer 2022 年 2 月 25 日
If the requirement is based on the 2nd decimal as it could be inferred in the problem statement, given a common input and no prompt as to whether to round up or down, round(round(val,2),1) will work. Otherwise the first answer with floor() and ceil() multiplying by a power of 10 and dividing again after rounding is an easy and common way to do this.
It is interesting that the accepted answer doesn't round the second value up as requested.
Stephen23
Stephen23 2022 年 2 月 26 日
編集済み: Stephen23 2023 年 4 月 27 日
"It is interesting that the accepted answer doesn't round the second value up as requested. "
It is simply because the actual value is less than 5 at that digit (not equal to 5 as it seems when displayed to a much lower precision):
minval = 0.4410
minval = 0.4410
maxval = 0.8450
maxval = 0.8450
fprintf('%.40f\n',minval,maxval)
0.4410000000000000031086244689504383131862 0.8449999999999999733546474089962430298328
So ROUND is working correctly, it is another reminder to always be aware of the behaviors of floating point numbers.
Given hints in the the OP's question, it seems like the correct answer was given by Rajesh here:

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

その他の回答 (1 件)

Rajesh
Rajesh 2015 年 7 月 24 日
移動済み: Stephen23 2023 年 4 月 27 日

6 投票

  1. floor(minval*10)/10
  2. ceil(maxval*10)/10

1 件のコメント

Stephen23
Stephen23 2023 年 4 月 27 日
+1 This actually returns what the OP requested:
minval = 0.4410;
maxval = 0.8450;
floor(minval*10)/10
ans = 0.4000
ceil(maxval*10)/10
ans = 0.9000

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

カテゴリ

質問済み:

2014 年 12 月 30 日

編集済み:

DGM
2023 年 5 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by