两种显示小数的方法 [英] two ways of displaying a decimal

查看:157
本文介绍了两种显示小数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有小数的列表:

let's say I have a list of decimals :

List<decimal> values;

和2函数来显示一个十进制:

and 2 function to display a decimal :

string DisplayPct(decimal pct)
{
   return pct.ToString("0.00 %");
}

string DisplayValue(decimal val)
{
   return val.ToString("0.00");
}

什么是执行这样的最佳机制,我可以知道哪个函数调用根据不同的价值?

What would be the best mechanism to implement so I could know which function to call depending on the value?

我本来希望有在C#中的typedef实例。这样一来,我可以声明的类型百分比和类型小数,将两者都表示十进制值,但后来我也知道如何展示基于其类型的值。

I would have liked to have for instance typedefs in C#. That way, I could have declared a type Percent and a type Decimal that would both represent decimal values, but then I could know how to display the value based on its type.

在C#中的任何等同?

感谢

推荐答案

裹%的一类。工作对你的一部分了一下。只要确保定义的隐运营商捕捉到小数,和其他一些操作和toString作为您的要求。

Wrap percent in a class. A bit of work on your part. Just make sure to define the implicit operator to capture decimals, and some of the other operations and ToString as you require.

这篇关于两种显示小数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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