Search code examples
iosobjective-ciphonemacosnsnumber

Convert Number to digits in Objective C


I have received duration from service. the duration is 0.73 so i have tried this

if (value >= 1.0 && value < 60.0) {
    double value = 0.73;
    double d = value * 1000;
    NSLog(@"milli seconds = %03d ms",(int)d);
    //Output is 730ms    --> which is correct. 
}

If i received 1.45 above 1.0

double value = 1.45
if (value >= 1.0 && value < 60.0) {
    double d = value * 1000;
    double sec = (int)value % 60;
    NSLog(@"%02d s:%012.f ms",(int)sec, d);
    //Output is 01 s: 000000001450 ms
}


The output should be as 01s:450ms

I need to change this to 01s:450ms. but i cant try this. any body help this. Thanks in Advance.


Solution

  • double value = 1.45;
    if (value >= 1.0 && value < 60.0) {    
        double sec = (int)value % 60;
        double d = (value - sec) * 1000;
        NSLog(@"%02ds:%.0f ms",(int)sec, d);
    }