2

In my model I want to format the output of a double. I'm trying to output a number like this:

100000000 as 100.000.000 (simply a dot separator)

But by using this (currency format)

[DisplayFormat(DataFormatString = "{0:C}")]

Result => 424.232.344 kr

I also get the currency symbol (depending on the culture, in my case "kr") and I don't want that. I simply want the dot separator but without the currency.

Any suggestions?

5
  • 1
    Does {0:#,#} work? -- See also Custom Numeric Format Strings Commented Jan 29, 2016 at 8:45
  • check this for more options msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx Commented Jan 29, 2016 at 8:46
  • @Corak, have tried. It results in spaces instead of dots. Commented Jan 29, 2016 at 8:52
  • @Ian Have checked it, does not contain my desired output. Commented Jan 29, 2016 at 8:52
  • 1
    @Ian - yes, that is probably, because the thousands-separator of your current(UI)culture has it defined like that. Sorry, I'm not versed enough in MVC to know how you can specify a different language. Usually, you can do something like 1000.ToString("#,#", CultureInfo.GetCultureInfo("de-DE")). But I think that's not possible with data annotations, because the culture isn't a compile constant. Commented Jan 29, 2016 at 8:58

1 Answer 1

5

Because "C" means currency in that format specifier and it uses CurrencySymbol of your CurrentCulture setting.

You can use The "N" format specifier instead.

[DisplayFormat(DataFormatString = "{0:N0}")]

Based on your comment, looks like your CurrentCulture using white space as NumberGroupSeparator but uses . as a CurrencyGroupSeparator.

In such a case, you can set it's NumberGroupSeparator to . and you will be fine.

Read: Is there a way of setting culture for a whole application? All current threads and new threads?

Sign up to request clarification or add additional context in comments.

5 Comments

Yes I am aware that C stands for currency. I Have tried your suggestion and it returns 424 232 344,00 (spaces instead of dots.)
@ChrisRun Updated my answer. Take a look.
Thanks, but unfortunately I'm not able to set the NumberGroupSeparator in this case, since the instance is readonly.
@ChrisRun NumberGroupSeparator property is not read-only but your culture setting might be. Have you ever check this question for this? stackoverflow.com/q/18728505/447156
My mistake, yes you are correct, the culture setting is readonly. Have not, will check it out, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.