これは、Webサービス「rateavg」から取得しています:「2.6111」
今、私はこれを文字列で取得しています。2.6 になる場合は 3 が表示され、2.4 または 2.5 になる場合は 2 が表示されるようにするにはどうすればよいですか?
これを取得する方法はありません。私を助けてください
これは、Webサービス「rateavg」から取得しています:「2.6111」
今、私はこれを文字列で取得しています。2.6 になる場合は 3 が表示され、2.4 または 2.5 になる場合は 2 が表示されるようにするにはどうすればよいですか?
これを取得する方法はありません。私を助けてください
これを試して
float f=2.6;
NSLog(@"%.f",f);
お役に立てれば。
これをチェックして
float floatVal = 2.6111;
long roundedVal = lroundf(floatVal);
NSLog(@"%ld",roundedVal);
私はこれを思いつきました、あなたのクエリのレプリカです:
NSString* str = @"2.611";
double duble = [str floatValue];
NSInteger final = 0;
if (duble > 2.5) {
final = ceil(duble);
}else{
final = floor(duble);
}
NSLog(@"%ld",(long)final);
したがって、どちらかceil
またはfloor
メソッドを使用する場合です。
編集:すべてのダブルスに必要なので:
NSString* str = @"4.6";
double duble = [str floatValue];
NSInteger final = 0;
NSInteger temp = floor(duble);
double remainder = duble - temp;
if (remainder > 0.5) {
final = ceil(duble);
}else{
final = floor(duble);
}
NSLog(@"%ld",(long)final);
私はそれをテストし、すべての桁で動作するコードの下で試してください、
NSString *str = @"2.7";
NSArray *arr = [str componentsSeparatedByString:@"."];
NSString *firstDigit = [arr objectAtIndex:0];
NSString *secondDigit = [arr objectAtIndex:1];
if (secondDigit.length > 1) {
secondDigit = [secondDigit substringFromIndex:1];
}
int secondDigitIntValue = [secondDigit intValue];
int firstDigitIntValue = [firstDigit intValue];
if (secondDigitIntValue > 5) {
firstDigitIntValue = firstDigitIntValue + 1;
}
NSLog(@"final result : %d",firstDigitIntValue);
または別の解決策 - 少し短い
NSString *str1 = @"2.444";
float my = [str1 floatValue];
NSString *resultString = [NSString stringWithFormat:@"%.f",my]; // if want result in string
NSLog(@"%@",resultString);
int resultInInt = [resultString intValue]; //if want result in integer
値を最も近い整数に丸めるには、 のroundf()
関数を使用しますmath
。
最初にインポートmath.h
:
#import "math.h"
例、
float ValueToRoundPositive;
ValueToRoundPositive = 8.4;
int RoundedValue = (int)roundf(ValueToRoundPositive); //Output: 8
NSLog(@"roundf(%f) = %d", ValueToRoundPositive, RoundedValue);
float ValueToRoundNegative;
ValueToRoundNegative = -6.49;
int RoundedValueNegative = (int)roundf(ValueToRoundNegative); //Output: -6
NSLog(@"roundf(%f) = %d", ValueToRoundNegative, RoundedValueNegative);
詳細については、こちらのドキュメントを参照してください。
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/roundf.3.html
NSNumberFormatter を使用して 10 進数値を丸めることができます。
NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
[format setPositiveFormat:@"0.##"];
NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.342]]);
NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.3]]);
NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.0]]);
対応する結果:
2010-08-22 15:04:10.614 a.out[6954:903] 25.34
2010-08-22 15:04:10.616 a.out[6954:903] 25.3
2010-08-22 15:04:10.617 a.out[6954:903] 25
NSString* str = @"2.61111111";
double value = [str doubleValue];
2.5 -> 3: int num = value+0.5;
2.6 -> 3: int num = value+0.4;
必要に応じて設定:
double factor = 0.4
if (value < 0) value *= -1;
int num = value+factor;
NSLog(@"%d",num);
NSString *value = @"1.23456";
float floatvalue = value.floatValue;
int rounded = roundf(floatvalue);
NSLog(@"%d",rounded);
より価値の高いラウンドを使用する場合は、使用してくださいceil(floatvalue)
価値の低いラウンドを使用する場合は、使用してくださいfloor(floatvalue)