ceci
ceci

Reputation: 429

Concat two integers

I am using MVC 4. I've got an MVC Form two fields AmtInDollars & AmtInCents. I want to concatenate the two. Eg

var tempDollarAmt = AmtInDollars + "" + AmtInCents; 

Example

I realized that if the AmtInCents is between 0 and 9 it ingnores the 0. So if I type 09 the output is 9 not 09.

I have tried doing an if statement below and still no luck.

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

Here my class

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

How can I do this?

Upvotes: 1

Views: 2289

Answers (2)

maraaaaaaaa
maraaaaaaaa

Reputation: 8193

//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;

Upvotes: 0

CaldasGSM
CaldasGSM

Reputation: 3062

you should use string.format to force padding when formating number

var tempDollarAmt = String.Format("{0}.{1:00}",AmtInDollars ,AmtInCents); 

Upvotes: 3

Related Questions