2011-01-15 3 views
37

Come potrei arrotondare un galleggiante all'intero più vicino in Objective-C:Objective-C Float A completare

Esempio:

float f = 45.698f; 
int rounded = _______; 
NSLog(@"the rounded float is %i",rounded); 

deve stampare "il galleggiante arrotondato è 46"

risposta

26

Il metodo consigliato è in questa risposta: https://stackoverflow.com/a/4702539/308315


risposta originale:

convertirlo in int dopo aver aggiunto 0,5.

Così

NSLog (@"the rounded float is %i", (int) (f + 0.5)); 

Edit: il modo in cui hai chiesto:

int rounded = (f + 0.5); 


NSLog (@"the rounded float is %i", rounded); 
+7

Questo produrrà solo un arrotondamento _downward_. Per ottenere <.5 rounds down e> = .5 rounds up: [NSString stringWithFormat: @ "% i", (int) round (f)] – mpemburn

+12

effettivamente no. Questo produrrà l'arrotondamento più vicino. –

+13

Questo non gestisce correttamente i numeri negativi. – AndrewCr

144

Utilizzare la funzione C standard della famiglia round(). roundf() per float, round() per double e roundl() per long double. È quindi possibile trasmettere il risultato al tipo intero di propria scelta.

+2

Penso che questo sia il modo migliore e più efficiente per farlo. –

+2

Diversamente dal metodo + 0.5, gestisce correttamente i numeri negativi. – SwiftArchitect

+0

Ehi @ JonathanGrynspan molte risposte in giro ci collegano alla tua risposta qui - sarebbe bello espanderla con alcuni esempi di come (e perché) usarli. – brandonscript

3

Controllare la pagina di manuale per rint()

0

Facciamo provato e nella verifica

//Your Number to Round (can be predefined or whatever you need it to be) 
float numberToRound = 1.12345; 
float min = ([ [[NSString alloc]initWithFormat:@"%.0f",numberToRound] floatValue]); 
float max = min + 1; 
float maxdif = max - numberToRound; 
if (maxdif > .5) { 
    numberToRound = min; 
}else{ 
    numberToRound = max; 
} 
//numberToRound will now equal it's closest whole number (in this case, it's 1) 
+0

Penso che questa risposta sia molto più utile di qualsiasi altro sopra in base alla domanda posta .. –

5

Il modo più semplice per arrotondare un galleggiante in Objective-C è lroundf:

float yourFloat = 3.14; 
int roundedFloat = lroundf(yourFloat); 
NSLog(@"%d",roundedFloat); 
+0

No, il più semplice è aggiungere 0,5 e troncare. Non devi ricordare "lroundf" per farlo, e sai esattamente cosa stai ricevendo. –

+4

@Hot Licks - A meno che non sia necessario arrotondare sia numeri positivi che negativi (il mio caso) in cui il metodo 0.5 è difettoso. – capikaw

3

Se nel caso vuoi arrotondare il valore float in intero qui sotto è il metodo semplice per arrotondare il valore float nell'obiettivo C.

int roundedValue = roundf(Your float value); 
1

Per rotonda float ad uso intero più vicino roundf()

roundf(3.2) // 3 
roundf(3.6) // 4 

È anche possibile utilizzare la funzione per ceil() sempre ottenere il valore superiore dal float.

ceil(3.2) // 4 
ceil(3.6) // 4 

E per il valore più basso floor()

floorf(3.2) //3 
floorf(3.6) //3