查找十进制值小数点后的位数,无论文化 [英] Find number of decimal places in decimal value regardless of culture

查看:157
本文介绍了查找十进制值小数点后的位数,无论文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有拉出来一个十进制值的小数号(作为int),这将是安全的跨越不同的文化信息用简明,准确的方式?

I'm wondering if there is a concise and accurate way to pull out the number of decimal places in a decimal value (as an int) that will be safe to use across different culture info?

例如:结果
19.0应该返回1,结果
27.5999应该返回4,结果
19.12应该返回2,结果
等等。

For example:
19.0 should return 1,
27.5999 should return 4,
19.12 should return 2,
etc.

我写了一个查询,做了一段时间的字符串分割找到小数位:

I wrote a query that did a string split on a period to find decimal places:

int priceDecimalPlaces = price.ToString().Split('.').Count() > 1 
                  ? price.ToString().Split('.').ToList().ElementAt(1).Length 
                  : 0;

但它发生,我认为这只会在使用该地区的工作'。作为小数点分隔符,因此在不同的系统非常脆弱。

But it occurs to me that this will only work in regions that use the '.' as a decimal separator and is therefore very brittle across different systems.

推荐答案

我使用乔的方式来解决这个问题:)

i use Joe's way to solve this issue :)

decimal argument = 123.456m;
int count = BitConverter.GetBytes(decimal.GetBits(argument)[3])[2];

这篇关于查找十进制值小数点后的位数,无论文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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