Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# number shaper for money amount like convert int 12333545 = 12,333,545

What is the easiest and fastest way of converting string or int to the money format

i mean like an integer = 21232221 when shaped = 21,232,221

c# 4.0 asp.net 4.0

thank you

the direct answer

    public static string NumberShaper(int irNumber)
{
    return (irNumber.ToString("N", System.Globalization.CultureInfo.InvariantCulture).Replace(".00",""));
}
like image 767
MonsterMMORPG Avatar asked Dec 17 '22 07:12

MonsterMMORPG


1 Answers

You can just call ToString with the format string of choice.

For example, in your case:

int value = 21232221;

string result = value.ToString("N");

This will place 21,232,221 as the value in result. To format as currency, use "C" (though this will add the currency specified, ie: $). There are many options for format strings - for details, see here.

like image 190
Reed Copsey Avatar answered Jan 17 '23 15:01

Reed Copsey