十进制转换为字符串没有逗号或点 [英] Convert decimal to string without commas or dots

查看:105
本文介绍了十进制转换为字符串没有逗号或点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,我需要一个小数量(钱)转换为数字的字串,即: 123,456.78 - > 12345678

In .NET, I need to convert a decimal amount (money) to a numbers-only string, i.e: 123,456.78 -> 12345678

我觉得

var dotPos = amount.ToString().LastIndexOf('.');
var amountString = amount.ToString().Remove(dotPos);

将解决我的问题,或者至少是它的一部分,但我希望它没有工作。 我试图做到这一点可能无需编写大量的code和使用已经被设计为类似用途的东西。

would solve my problem, or at least part of it, but it didn't work as I expected. I'm trying to do this possibly without writing a lot of code and using something already designed for a similar purpose.

推荐答案

您可以做到这一点是这样的:

You could do it like this:

var amountString = string.Join("", amount.Where(char.IsDigit));

使用 char.IsDigit 方法将保护您免受其他未知的符号,比如 $ ,也将与其他工作货币格式。底线,你不知道的完全什么字符串总是看起来像那么它的安全这样的。

Using the char.IsDigit method will protect you from other unknown symbols like $ and will also work with other currency formats. The bottom line, you don't know exactly what that string will always look like so it's safer this way.

这篇关于十进制转换为字符串没有逗号或点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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