【问题标题】:How to code metric prefix (kilo,mega,giga etc) and do calculation on them?如何编码公制前缀(千、兆、千兆等)并对其进行计算?
【发布时间】:2014-10-06 02:11:17
【问题描述】:

我正在尝试用 untiy 和 c# 制作一个关于数字的游戏。

但是我需要大数字(千、兆、千兆等)的公制前缀

到目前为止,我一直在做这样的事情:

normal += 9;
  if(normal >= 999)
  {
    normal = 0;
    kilo += 1;
  }
  if(kilo >= 999)
  {
    kilo = 0;
    mega += 1;
  }

但我遇到了一个问题,当数字变为 981 990 999 时,正常数字会重置而不是 1008

还有如何计算前缀?

编辑#1:

当我点击对象“ASDASD”时我必须对象(统一)我得到钱,当我点击对象“QQQ”时我买东西

此代码适用于 ASDASD

public float normal;
    public float kilo;
    public float mega;
    public float x;

    public GUIText Displayer;



    void Start () 
    {



    }

    void OnMouseDown ()
    {

        if (gameObject.name=="ASDASD")
        {
        normal =normal + 90000;

        if(normal > 99999)
        {

           x=normal-100000;

           normal = 0;

           kilo = kilo + 1 + x/100000f;


        }
        }
    }


    void Update ()

    {

        if (kilo==0)

           Displayer.text = "" + normal.ToString("n0");

        else if (kilo >=1)

            Displayer.text="" + (kilo*100+normal/1000).ToString("n0") + "K";




    }
}

现在我该怎么做第二个目标代码? (买东西)

【问题讨论】:

  • 那么981990999 作为输入的预期结果是什么?
  • 变量normalkilomega代表什么? normal 最初是如何设置的?你想用这些计算什么?您能否提供一个我们可以自己编译和运行的完整代码示例来提供更多上下文?
  • 981 Mega 或 982 Mega(其中之一就是我想要的)
  • 好的,我想我必须解释更多,请尽快查看我的原始帖子
  • 查看编辑#1 了解更多信息

标签: c# unity3d prefix


【解决方案1】:

我认为你选择了错误的方式。您应该有一个价格变量和一个将其转换为文本表示的函数。 Answer for that

【讨论】:

    【解决方案2】:

    我最近创建了这个函数:

    string[] prefixeSI = {"y", "z", "a", "f","p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y"};
    string numStr(double num)
    {
        int log10 = (int)Math.Log10(Math.Abs(num));
        if(log10 < -27)
          return "0.000";
        if(log10 % -3 < 0 )
            log10 -= 3;
        int log1000 = Math.Max(-8, Math.Min(log10 / 3, 8));
    
        return ((double)num / Math.Pow(10, log1000 * 3)).ToString("###.###" + prefixeSI[log1000+8]);
    }
    
    
    Console.WriteLine(numStr(1000000)); // 1M
    Console.WriteLine(numStr(100000));  // 100k
    Console.WriteLine(numStr(10000));   // 10k
    Console.WriteLine(numStr(1000));    // 1k
    Console.WriteLine(numStr(100));     // 100
    Console.WriteLine(numStr(10));      // 10
    Console.WriteLine(numStr(1));       // 1
    Console.WriteLine(numStr(0));       // 0.000
    Console.WriteLine(numStr(0.1));     // 100m
    Console.WriteLine(numStr(0.01));    // 10m
    Console.WriteLine(numStr(0.001));   // 1m
    Console.WriteLine(numStr(0.0001));  // 100µ
    
    //Over Yotta
    Console.WriteLine(numStr(4545689486541536356525425482.64786));// 4545.689Y
    //Under yocto
    Console.WriteLine(numStr(-0.000000000000000000000001));       // -1y
    Console.WriteLine(numStr(-0.000000000000000000000000001));    // -.001y
    Console.WriteLine(numStr(-0.0000000000000000000000000001));   // 0.000
    

    这看起来不错,但是当你有一个低于 0.001y 的数字时,你可以看到限制:它什么也不写! (但是,我很少使用像 10E-27 =P 这样的数字)

    当然,您可以删除“If(log10

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多