0

<p class="card"> Credit Limit: <b style="color: #00e500 ;"> $@Model.CreditLimit.ToString().ToLocaleString()</b> </p>

I am changing an Int to a String using ToString() Then I am trying to format the number I am getting using ToLocaleString(). The number looks like this: 80567. I am trying to format it to look like this: 80,567.

The error I get is "string does not contain a definition for "ToLocaleString"

Thoughts?

EDIT: Going to have this issue closed. Found where my error resided. Please refer to this link if you have any questions on the topic: Link to StackOverFlow issue

Dominic Gozza
  • 81
  • 1
  • 1
  • 9

4 Answers4

3

I see that you want to show money amount, you should use currency NumberFormatInfo for this. First get current CultureInfo or create it for specific language-country:

var culture = Thread.CurrentThread.CurrentCulture;
// or
var culture = new CultureInfo("en-US"); 

Then use it to format your number:

var creditLimit = 100;
var creditLimitFormatted = creditLimit.ToString("c", culture);

The "c" stands for "currency", you can see other possible options in the documentation of NumberFormatInfo.

Formatting Numeric Data for a Specific Culture

Michał Żołnieruk
  • 2,095
  • 12
  • 20
1

@Model.CreditLimit - this is Integer

@Model.CreditLimit.ToString() - this is a String

now you are trying to execute the ToLocaleString() which clearly from the error

"string does not contain a definition for "ToLocaleString"

To do what you want to archive try the solutions below:

String.Format("{0:n}", @Model.CreditLimit); //Output: 80,567.00

string.Format("{0:n0}", @Model.CreditLimit); // Output: 80567

etrupja
  • 2,710
  • 6
  • 22
  • 37
0

That's because the int (Int32) class is not formattable, that is, it's representation does not depend on the culture, for example. You have to roll out your formatting code.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
0

This article gives a nice overview of the different format types available. It sounds like you want the Numeric Format Specifier. You'd use it like this

int intValue = 123456789;
Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
// Displays 123,456,789.0 
Gareth
  • 913
  • 6
  • 14