在C#编写大型数字文字的一种方式 [英] A way of writing large numeral literals in C#

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

问题描述

我有这样一个方法:

 前缀GetPrefix(十进制值)
{
如果(价值> 11000000000000000000)
返回Prefix.CosmicBig;
如果(价值>千兆)
返回Prefix.ReallyBig;
如果(价值> 30000亿),
返回Prefix.Big;
如果(价值> 5000)
返回Prefix.Okay;
如果(价值> 750000)
返回Prefix.MostlyNormal;
如果(价值> 750000)
返回Prefix.SoSo;
如果(价值> 750)
返回Prefix.Small;
返回Prefix.MiserablySmall;
}

的精确值并不重要。重要的是,它们有时改变(前缀被用于绘制和一些文本区域改变大小在开发中)。我在寻找写作的方式,由人工改变其可读性是轻松地将这些文字的方式,而不必指望所有的零。用隔板将是很好。我想过写 11 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 ,但这只是勉强更易于管理。使用 Math.Pow()做它好一点,但我不舒服,使用这种计算来定义常量。


解决方案

您可以引入扩展方法 INT



<预类=郎-CS prettyprint-覆盖> 750.Thousand();
5.Million();
100.Billion();



这些方法的实现很简单:

 公共静态INT千(此int值)
{
返回值* 1000;
}

公共静态INT万元(此int值)
{
返回value.Thousand()* 1000;
}

//等

确认返回对于方法更大号的相应数据类型:

 公共静态长亿美元(此int值)
{
返回value.Million()* 1000;
}



可惜的是,你将不得不写每一个整数或浮点数类型的方法要支持。



拥有全套这样的扩展方法将允许你在relativly自然的方式表达你的大量涌现,甚至当它不只是全零的结束:

  100.Billion()+ 30.Thousand()+ 300 // == 100000030300 

如果你想获得幻想,你甚至可以考虑嵌套他们:

  100.Billion(30.Thousand(300))

但我认为会失去一些表现,因为人们会怀疑参数意思



不过,实施应该是这样的:

 公共静态长亿美元(此int值,长补充)
{
返回value.Million()* 1000 +增加;
}






使用这些扩展方法有一个小小的缺点:您的号码已不再编译时间常数。它们在运行时计算。在绝大多数情况下,这不应该是一个问题。


I have a method like this:

Prefix GetPrefix(decimal value)
{
    if(value > 11000000000000000000)
        return Prefix.CosmicBig;
    if(value > 1000000000000000)
        return Prefix.ReallyBig;
    if(value > 3000000000000)
        return Prefix.Big;
    if(value > 50000000)
        return Prefix.Okay;
    if(value > 750000)
        return Prefix.MostlyNormal;
    if(value > 750000)
        return Prefix.SoSo;
    if(value > 750)
        return Prefix.Small;
    return Prefix.MiserablySmall;
}

The exact values are not important. What matters is that they are sometimes changed (the prefixes are used for drawing and some text areas change sizes in development). I'm looking for a way of writing these literals in a way that's easily readably by a human changing it, without having to count all the zeroes. A separator would be nice. I thought about writing 11 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, but that's only barely more manageable. Using Math.Pow() does it a little better, but I'm not comfortable with using such calculations to define constants.

解决方案

You can introduce extension methods for int:

750.Thousand();
5.Million();
100.Billion();

The implementation of these methods is simple:

public static int Thousand(this int value)
{
    return value * 1000;
}

public static int Million(this int value)
{
    return value.Thousand() * 1000;
}

// etc.

Make sure to return the appropriate data type for methods for bigger numbers:

public static long Billion(this int value)
{
    return value.Million() * 1000;
}

Unfortunatelly, you will have to write those methods for every integral or floating point type you want to support.

Having a full set of such extension methods would allow you to express your large numbers in relativly natural ways, even when it's not just all zeros at the end:

100.Billion() + 30.Thousand() + 300 // == 100,000,030,300

If you want to get fancy, you could even think about nesting them:

100.Billion(30.Thousand(300))

But I think that would lose some expressiveness, because people would wonder what the parameter means.

Still, implementation would look like this:

public static long Billion(this int value, long add)
{
    return value.Million() * 1000 + add;
}


Using these extension methods has one little downside: Your numbers are no longer compile-time constants. They are calculated at runtime. In the vast majority of cases, this shouldn't be a problem.

这篇关于在C#编写大型数字文字的一种方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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