Since Umbraco doesn't do decimal numbers, can I store them in text properties and use AsDecimal() to convert them? So far my experiments haven't succeded and I'm wondering if I've entirely missed the boat again. I've been assuming that if I find it on the ASP.NET site, I can use it here. Is that true? Or does Umbraco use a limited subset of it.
My feeling says, yes, you can store a value as text and convert it to decimal when you need. Have you tried the intrinsic decimal.Parse(str)/TryParse(str, out dec) methods? I'm not sure about any "AsDecimal" methods, but there is a Convert.ToDecimal(str) that might also do the same thing... Paste a code sample and let's see if we can help!
If the property value is returned as a string then, yes, AsDecimal() should work. However, if your string can't be cast to a Decimal value then it will return 0.
To determine the actual type your property is sored as try:
@Model.MyProperty.GetType()
To force the property to be a string before conversion you could try:
Thanks much for the replies. I'm still very weak on the fundamentals, so please pardon my ignorance.
@Funka!, I haven't tried Parse, it looked over my head. When I try Convert.ToString, I get the 'Error loading MacroEngine script'.
@Dan, you're right, GetType returns 'System.Decimal' although it's a textstring property, which I haven't found a good explanation for, if Umbraco doesn't do decimal. When I try ToString...AsDecimal, I also get the 'Error loading MacroEngine script'.
The goal is to store these values on each page, then present an average, so if 9 pages have values of 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9 and 2 (to keep it simple), then I'm aiming at getting the sum of those (15.4), getting the count of pages with values (9) and dividing the sum by the count for the average (15.4 / 9 = 1.721111111111111), and probably want to round that down to 1.72 (pardon if I've goofed the math).
Thanks Dan. I had tried that, just forgot to paste that sample. I've tried it every which way I can think of now and it seems like any time I put AsDecimal() in there anywhere, whether after ToString() or just by itself, it errors with 'Error loading MacroEngine script'. Has anyone else ever used AsDecimal(), or would someone mind testing if it works in Umbraco? FWIW, ToString() seems to work fine. Maybe Umbraco really, really hates decimals???
No, but thanks Dan. I had that much in there already, I was thinking maybe there was something else. I've seen some scripts that have maybe 4 different @using lines at the top. I haven't found anything that explains what the various options are for the @using lines and how/when to use them. Seems like if the asp.net site says Razor can do it that it oughtta work....
Can I use AsDecimal() in Umbraco?
Since Umbraco doesn't do decimal numbers, can I store them in text properties and use AsDecimal() to convert them? So far my experiments haven't succeded and I'm wondering if I've entirely missed the boat again. I've been assuming that if I find it on the ASP.NET site, I can use it here. Is that true? Or does Umbraco use a limited subset of it.
http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax
My feeling says, yes, you can store a value as text and convert it to decimal when you need. Have you tried the intrinsic decimal.Parse(str)/TryParse(str, out dec) methods? I'm not sure about any "AsDecimal" methods, but there is a Convert.ToDecimal(str) that might also do the same thing... Paste a code sample and let's see if we can help!
If the property value is returned as a string then, yes, AsDecimal() should work. However, if your string can't be cast to a Decimal value then it will return 0.
To determine the actual type your property is sored as try:
To force the property to be a string before conversion you could try:
Thanks much for the replies. I'm still very weak on the fundamentals, so please pardon my ignorance.
@Funka!, I haven't tried Parse, it looked over my head. When I try Convert.ToString, I get the 'Error loading MacroEngine script'.
@Dan, you're right, GetType returns 'System.Decimal' although it's a textstring property, which I haven't found a good explanation for, if Umbraco doesn't do decimal. When I try ToString...AsDecimal, I also get the 'Error loading MacroEngine script'.
The goal is to store these values on each page, then present an average, so if 9 pages have values of 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9 and 2 (to keep it simple), then I'm aiming at getting the sum of those (15.4), getting the count of pages with values (9) and dividing the sum by the count for the average (15.4 / 9 = 1.721111111111111), and probably want to round that down to 1.72 (pardon if I've goofed the math).
I was just trying this to try and get started:
I was alternatively commenting out the last two lines for testing.
If you want to convert a property to a string you just call ToString() - no need for the Convert part ie.
@Model.annBalMWE.ToString()
@Model.annBalMWE.ToString().AsDecimal()
The other way to ensure your value is a decimal would be:
@Convert.ToDecimal(Model.annBalMWE)
However, this would throw an error if the value could not be converted (say it was empty).
Thanks Dan. I had tried that, just forgot to paste that sample. I've tried it every which way I can think of now and it seems like any time I put AsDecimal() in there anywhere, whether after ToString() or just by itself, it errors with 'Error loading MacroEngine script'. Has anyone else ever used AsDecimal(), or would someone mind testing if it works in Umbraco? FWIW, ToString() seems to work fine. Maybe Umbraco really, really hates decimals???
Ran into this post, which suggests using 'decimal', maybe:
Tried that and several variations, still get that 'Error loading MacorEngine script..'.
Grrrr....
Could it be erroring because I should be @using something else? I've never found an explanation of how to use those properly.
Matthew, try adding following to top of scripts:
Does that help?
No, but thanks Dan. I had that much in there already, I was thinking maybe there was something else. I've seen some scripts that have maybe 4 different @using lines at the top. I haven't found anything that explains what the various options are for the @using lines and how/when to use them. Seems like if the asp.net site says Razor can do it that it oughtta work....
is working on a reply...