我将数据保存在字符串中,当我尝试使用doubleValue方法将字符串转换为double或浮点数时.我得到1.这是我的代码.
Nsstring *price = self.priceFIEld.text; //here price = 1,250.50double priceInDouble = [price doubleValue];
在这里,我得到1而不是1250.50.
我想,问题是逗号,但我无法摆脱那个逗号,因为它来自数据库.任何人都可以帮我把这个字符串格式转换为double或float.
解决方法 对此的解决方案实际上是删除逗号.虽然您最初从数据库中获取这些逗号,但您可以在转换之前删除它们.添加它作为从DB获取数据并将其转换为double之间的额外步骤:Nsstring *price = self.priceFIEld.text; //price is @"1,250.50"Nsstring *priceWithoutCommas = [price stringByReplacingOccurrencesOfString:@"," withString:@""]; //price is @"1250.50"double priceInDouble = [priceWithoutCommas doubleValue]; //price is 1250.50总结
以上是内存溢出为你收集整理的ios – 使用逗号时将String转换为double全部内容,希望文章能够帮你解决ios – 使用逗号时将String转换为double所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)