2011-12-03 63 views
0

我試圖從文本字段獲取用戶輸入,並將其格式化爲核心數據的雙精度值。目前我的代碼看起來像:NSNumberFormatter:string to Double

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 

NSNumber *weightDouble = [numberFormatter numberFromString:@"weight.text"]; 

但是,如果我打印weightDouble我得到0,如果我打印出從UI輸入我得到了正確數量的文本。關於我在這裏做錯的任何想法?我上構建沒有錯誤和它的運作,節省精(除節省0無論輸入)

+0

看'NSDecimalNumber' – syclonefx

回答

3

首先: (如果「weight」是UITextField

NSNumber *weightDouble = [numberFormatter numberFromString:weight.text]; 

隨着numberFromString:@"weight.text"您將獲得文本'weight.text'的值實際上爲0.

但是,爲什麼不只是double weightDouble = weight.text.doubleValue? (除了可能的本地化問題)

相關問題