格式货币字符串到整数 [英] Format Currency string to integer

查看:138
本文介绍了格式货币字符串到整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用货币格式类似$ 35.00,这已被转换为35。谁能帮助一个字符串?这是可能使用的String.Format {}


解决方案

  int值= INT检索。解析($ 35.00,NumberStyles.Currency); 



应该给你你需要的答案。



然而,像$ 35.50的值转换成整数可能不会回报你希望它是什么,因为整数不支持部分数(十进制)。你没有指定在这种情况下会发生什么





如果你想在这种情况下获得的35.5值,你可能想使用十进制类型。

 十进制值= decimal.Parse($ 35.00,NumberStyles.Currency); 

请注意,您必须非常用金钱和浮动打交道时谨慎点精度。


I'm have a string with currency format like $35.00 and this has to be converted to 35. Can anyone help? Is that possible to retrieve using String.Format{ }

解决方案

int value = int.Parse("$35.00", NumberStyles.Currency);

Should give you the answer you need.

However, a value like $35.50 converted to an integer will likely not return what you want it to, as Integers do not support partial (decimal) numbers. You didn't specify what to expect in that situation.

[EDIT: Changed double to decimal which is safer to use with currency]

If you want to get a value of 35.5 in that situation, you might want to use the decimal type.

decimal value = decimal.Parse("$35.00", NumberStyles.Currency);

Note that you have to be very careful when dealing with money and floating point precision.

这篇关于格式货币字符串到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆