c# - Increasing the value by 1 if it has decimal place? -
my scenario if
47/15= 3.13333 i want convert 4, if result has decimal want increase result 1, right doing like
float res = ((float)(62-15) / 15); if (res.tostring().contains(".")) { string digit=res.tostring().substring(0, res.tostring().indexof('.')); int incrementdigit=convert.toint16(k) + 1; } i want know there shortcut way or built in function in c# can fast without implementing string functions.
thanks lot.
do mean want perform integer division, rounding up? suspect want:
public static int dividebyfifteenroundingup(int value) { return (value + 14) / 15; } this avoids using floating point arithmetic @ - allows value isn't exact multiple of 15 rounded up, due way integer arithmetic truncates towards zero.
note not work negative input - example, if passed in -15 return 0. fix with:
public static int dividebyfifteenroundingup(int value) { return value < 0 ? value / 15 : (value + 14) / 15; }
Comments
Post a Comment