Hi,
i want to floor a double by its decimal place with variable decimal length (in iphone sdk).
here some examples to show you what i mean
NSLog(@"%f",[self floorMyNumber:34.52462 toPlace:2); // should return 34.52
NSLog(@"%f",[self floorMyNumber:34.52662 toPlace:2); // should return 34.52
NSLog(@"%f",[self floorMyNumber:34.52432 toPlace:3); // should return 34.524
NSLog(@"%f",[self floorMyNumber:34.52462 toPlace:3); // should return 34.524
NSLog(@"%f",[self floorMyNumber:34.12462 toPlace:0); // should return 34.0
NSLog(@"%f",[self floorMyNumber:34.92462 toPlace:0); // should return 34.0
any ideas how to do this?
solution
-(double)floorNumberByDecimalPlace:(float)number place:(int)place {
return (double)((unsigned int)(number * (double)pow(10.0,(double)place))) / (double)pow(10.0,(double)place);
}