Wednesday 15 June 2011

function - C: How do I make a number always round up -



function - C: How do I make a number always round up -

i'm wanting create function calculates cost based on time. time less 3 hours, homecoming flat rate charge of $2. each hr or part of hr on that, charge additional $0.50. hours input user float info type 2 decimal places. how can create round time whole hour? here's code have far function:

int calculatecharges(float hours) { if (hours <= 3){ homecoming 2.00 } else

first of all, utilize info type int, cannot hold fractional values (and implicitly round them towards zero, before function ever called.) should utilize double instead, since proper datatype fractional numbers.

you want utilize ceil(x) function, gives nearest whole number larger or equal x.

#include <math.h> double calculatecharges(double hours) { if (hours <= 3) { homecoming 2.00; } else { // homecoming $2.00 first 3 hours, // plus additional $0.50 per hr begun after homecoming 2.00 + ceil(hours - 3) * 0.50; } }

c function rounding

No comments:

Post a Comment