文字列をdoubleに変換し、いくつかの計算を行った後、それを文字列に戻します。
Objective-Cでこれを行うにはどうすればよいですか?
doubleを最も近い整数に丸める方法はありますか?
文字列をdoubleに変換し、いくつかの計算を行った後、それを文字列に戻します。
Objective-Cでこれを行うにはどうすればよいですか?
doubleを最も近い整数に丸める方法はありますか?
回答:
あなたはNSStringをdoubleに変換することができます
double myDouble = [myString doubleValue];
次に、最も近いintへの丸めを次のように行うことができます
int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))
文字列に戻すためのより効率的な方法があるかどうか、正直なところわかりません
NSString* myNewString = [NSString stringWithFormat:@"%d", myInt];
[[NSNumber numberWithInt:myInt] stringValue]
実際に文字列から数値に正しく変換するには、文字列をNSNumberFormatter
読み取っているロケール用に構成されたのインスタンスを使用する必要があります。
ロケールが異なれば、数値のフォーマットも異なります。たとえば、世界の一部の地域ではCOMMA
小数点が区切り記号として使用されていますが、その他の地域ではPERIOD
-桁区切り記号(使用されている場合)が逆になっています。スペースの場合を除きます。またはまったく存在しない。
それは本当に入力の出所に依存します。最も安全なNSNumberFormatter
方法は、入力をフォーマットする方法を設定し-[NSFormatter numberFromString:]
、NSNumber
ことです。変換エラーを処理したい場合は、-[NSFormatter getObjectValue:forString:range:error:]
代わりに使用できます。
olliejの答えに追加すると、intからNSNumber
's を使用して文字列に変換できますstringValue
。
[[NSNumber numberWithInt:myInt] stringValue]
stringValue
NSNumber
呼び出すdescriptionWithLocale:nil
、あなたの値のローカライズされた文字列表現を与えます。が[NSString stringWithFormat:@"%d",myInt]
適切にローカライズされたの表現を提供するかどうかはわかりませんmyInt
。
以下は、ローカライズされた数値文字列(xCode 3.2.4、osX 10.6)を読み取るNSNumberFormatterの実用的なサンプルです。注意: "8,765.4"のような末尾の空白は処理できますが、これは先行空白を処理できず、浮遊テキスト文字を処理できません。(不正な入力文字列:「8」、「8q」、「8 q」。)
NSString *tempStr = @"8,765.4";
// localization allows other thousands separators, also.
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
// next line is very important!
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; // crucial
NSNumber *tempNum = [myNumFormatter numberFromString:tempStr];
NSLog(@"string '%@' gives NSNumber '%@' with intValue '%i'",
tempStr, tempNum, [tempNum intValue]);
[myNumFormatter release]; // good citizen
nil
値を返しますNSNumber*
。先頭の空白と迷子のテキスト文字に関する問題を再現できません。IMOは正常に機能しています。" 28.5 "
28.5として解析し、そして"gibberish28.5"
、"28.5gibberish"
、および"gibberish28.5gibberish"
すべて解析できないです。" 2 8 . 5. "
解析不可能ですが、それは珍しいことではありません。
// Converting String in to Double
double doubleValue = [yourString doubleValue];
// Converting Double in to String
NSString *yourString = [NSString stringWithFormat:@"%.20f", doubleValue];
// .20f takes the value up to 20 position after decimal
// Converting double to int
int intValue = (int) doubleValue;
or
int intValue = [yourString intValue];
数値から文字列への変換については、新しいリテラル構文(XCode> = 4.4)を使用すると、もう少しコンパクトになります。
int myInt = (int)round( [@"1.6" floatValue] );
NSString* myString = [@(myInt) description];
(それをNSNumberとしてボックス化し、NSObjectsの記述メソッドを使用して文字列に変換します)
テキストフィールドに入力されたテキストを整数に変換します
double mydouble=[_myTextfield.text doubleValue];
最も近いdoubleに丸める
mydouble=(round(mydouble));
最も近い整数に丸める(正の値のみを考慮)
int myint=(int)(mydouble);
doubleからstringへの変換
myLabel.text=[NSString stringWithFormat:@"%f",mydouble];
または
NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];
intからstringへの変換
myLabel.text=[NSString stringWithFormat:@"%d",myint];
または
NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];