7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

c++整数値の割り算(切り捨て・切り上げ・四捨五入)

Last updated at Posted at 2020-07-09

c++で割り算した時の切り捨て・切上げ・四捨五入の方法(intの場合)
・切り捨て:そのまま実行

int x;
int y;
int ans;
ans = x / y;

暗黙の型変換で切り捨てされます。

・切り上げ

int x;
int y;
int ans;
ans = (x + y - 1) / y;

・四捨五入


int x;
int y;
int ans;
ans = (double)x / double(y) + 0.5

四捨五入はdouble型やfloat型にキャストしないといけないですね。
以上備忘録

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?