Question: We have a requirement in app that we need variable number of decimal places for a field, rule for number of decimal places is returned from web service and user can change that rule from web portal

suppose `float number = 125.879601223`

what i need, if

```
int numberOfDecimalPlaces = 4
NSString *convertedString = @"125.8796"

```

or,

```
int numberOfDecimalPlaces = 2
NSString *convertedString = @"125.87"

```

Need a way to convert string based on `numberOfDecimalPlaces` rule, thanks

Answer: 
You can use:

```
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:numberOfDecimalPlaces];
[formatter setRoundingMode: NSNumberFormatterRoundUp];

NSString * convertedString = [formatter stringFromNumber:[NSNumber numberWithFloat: number]];

```
Use the `*` modifier in the string format:

```
NSString *convertedString = [NSString stringWithFormat: @"%.*f", numberOfDecimalPlaces, number];

```