连接两个整数

Concat two integers

我正在使用 MVC 4。我有两个字段的 MVC 表单 AmtInDollars 和 AmtInCents。我想将两者连接起来。例如

var tempDollarAmt = AmtInDollars + "" + AmtInCents; 

例子

我意识到如果 AmtInCents 在 0 和 9 之间,它会忽略 0。所以如果我输入 09,输出是 9 而不是 09。

我试过在下面做一个 if 语句,但仍然没有成功。

if(Products.AmtInCents < 10)
                {
                    var tempCents = 0;
                    Products.AmtInCents = 00;
                }

这是我的 class

public decimal? AmtInDollars { get; set; }
public decimal? AmtInCents { get; set; }

我该怎么做?

你应该在格式化数字时使用string.format强制填充

var tempDollarAmt = String.Format("{0}.{1:00}",AmtInDollars ,AmtInCents); 
//Input: 100 in dollars
int dollars = 100;
//Input: 00 in cents
int cents = 0;

//OutPut: 1000 // missing 1 zero due to input being 00.
int output = dollars * 100 + cents;