1

I am receiving a compiler error:

"Method xxx has parameter value of type "System.Decimal". "System.Decimal" is not a valid Windows Runtime must return only Windows Runtime types."

Is there anyway I can use a decimal? or should I just store the value as cents in an integer?

Cool Breeze
  • 1,289
  • 11
  • 34
  • It seems like this (the question of decimal versus double in financial applications) is addressed by the number of other questions on the same topic: http://stackoverflow.com/search?q=double+versus+decimal. The short answer is "don't do that". You'd be better off using a custom fixed-point data type (it boggles my mind many of the things they left out of WinRT...System.Decimal is definitely one of them) – Peter Duniho Nov 07 '14 at 01:15
  • Well I was always under the impression that I should use Decimal for money. I guess I am a bit stumped as to why I can't use Decimal for my brokered run time component. Can you please expand on this custom fixed-point data type? – Cool Breeze Nov 07 '14 at 01:22
  • You _should_ use Decimal for money, if it's available. It addresses rounding errors that exist in floating point formats. You can research fixed-point, as there is lots on the web about it. The short version is that, for example, you can keep integers that represent pennies, or even hundredths of a penny; then add/subtract works normally, and of course you have to track the movement of the decimal for multiplication and division. – Peter Duniho Nov 07 '14 at 01:39
  • Decimal exists in WinRT. – Nate Diamond Nov 08 '14 at 00:51
  • Not when building a windows runtime component - http://msdn.microsoft.com/en-us/library/windows/apps/br230301%28v=vs.110%29.aspx I think I am just gonna store it as cents in an integer. – Cool Breeze Nov 10 '14 at 03:45
  • Especially based on that last comment and edit - I don't see how it's a duplicate of the other question. This is specifically about problems using `Decimal` in WinRT, not about `decimal` vs. `double`. – Filip Skakun Nov 10 '14 at 06:05

2 Answers2

0

I found the easiest solution was to store it as cents in an integer.

I believe this may not work for all the currencies in the world but I am only concerned with dollars so its not an issue in this particular scenario.

An alternative is to implement a "money" pattern - http://martinfowler.com/eaaCatalog/money.html

Cool Breeze
  • 1,289
  • 11
  • 34
0

Actually, I found the easiest way is to use double for the return values and parameters of my WinRT component.

This is what I have in my C# Class Library

public static double Round(double value, int digits)
{
    // do stuff
}

This is the code of a C# Windows Runtime Portable Class Library I created as a wrapper to be called from my JavaScript code

public static double Round(double val, int digits)
{
    return MyCSharpClassLib.Class.Round(val, digits);
}

And from my JavaScript project, this is what I call

var round = WindowsRuntimeWrapper.Class1.round(3.1415926549, 3);

And it works just fine!

sebagomez
  • 9,501
  • 7
  • 51
  • 89