2

I've got a decimal value, which is stored in my database (SQL Server 2008) with a precision of 13, and a scale of 10.

Examples:

10.6157894734
68.0750000000
96.8723684210

Basically, the numbers represent a "score" out of 100.

Now, i want to display/represent this value a different way, depending on the culture.

For example, in AU, i want to display the value out of 100, rounded up to 2 decimal places.

So in the above example:

10.62
68.08
96.87

But in US, i want to display the value out of 10, rounded up to 1 decimal place. So in the above example:

1.1
6.8
9.7

Can this be done with a resource-file, e.g doing something like:

return score.ToString(Resources.Global.ScoreFormat);

Where it could be stored as "#.##" in en-US, but "#.#" in en-AU?

I'm pretty sure it can't, since i'm not only rounding, but transforming using Math? (e.g value / 10 for AU) But thought i'd ask the question.

I'm trying to avoid an ugly if statement which checks the current culture and does math/rounding manually.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • Hope `1.6` is a typo and is actually `1.1` ? – V4Vendetta Apr 02 '12 at 06:31
  • @V4Vendetta - haha, yup. – RPM1984 Apr 02 '12 at 06:32
  • I suppose the question is not about implementing custom ToString for the class score belongs to, able to handle what you need... Though... It would be interesting if the "formatter" could *trigger* methods of a class to handle data to be formatted, e.g. "#{CountryDepFmt}" would instantiate CountryDepFmt (conforming to some known interface) that would return the value formatted and also manipulated someway, e.g. if region is Au return (value/10).ToString("#.#"); or whatever the correct C# syntax is. – ShinTakezou Apr 02 '12 at 06:44

2 Answers2

2

You will need to put a modifier and a format string in your resources so that you can do something like

return (score * Resources.Global.ScoreModifier).ToString(Resources.Global.ScoreFormat);
Phil
  • 42,255
  • 9
  • 100
  • 100
1

Accepting Phil's answer, but this is the actual modifier/format for anyone who cares:

Code:

var formattedScore = (score / Convert.ToInt32(Global.ScoreModifier)).ToString(Global.ScoreFormat))

Global.resx

ScoreFormat     n1  
ScoreModifier   10  

Global.en-au.resx

ScoreFormat     n2  
ScoreModifier   1   

Seems to work..anyone spot a problem?

RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • For some reason I was thinking you could add a numeric resource - your solution is fine. – Phil Apr 02 '12 at 06:55
  • @Phil - numeric? I thought all C# resource strings were in fact, strings..therefore the int coercion is neccessary? – RPM1984 Apr 02 '12 at 06:58
  • Yes you're correct, I was trying to say I wasn't thinking straight :) – Phil Apr 02 '12 at 06:59