I need to do some simple calculation in my view, from numbers input to textfields in Umbraco. An input field is a "string" by default, as i understand. So i need to convert it to a calculatable format.
Converting the string to int works fine, but removes the decimals, if there are any casuing miscalculaton.
Converting to float works fine if the number has decimal, but fails if the number is actually an integer...
How do i bulletproof the conversion so numbers like 12 or 12.3 or 12.30 or 00 or 0.0 all work in the calculation?
string to float in razor
I need to do some simple calculation in my view, from numbers input to textfields in Umbraco. An input field is a "string" by default, as i understand. So i need to convert it to a calculatable format.
Converting the string to int works fine, but removes the decimals, if there are any casuing miscalculaton.
Converting to float works fine if the number has decimal, but fails if the number is actually an integer...
How do i bulletproof the conversion so numbers like 12 or 12.3 or 12.30 or 00 or 0.0 all work in the calculation?
It should "work" easily. Can you provide some code which is not working?
It did, the error was elsewhere, but no less strange. I needed to calculate this:
But for some reason i got errors when trying to do it in one go, so i ended up with:
which works :)
So... your issue has magically solved by splitting into lines? :-)
If you need decimals, you multiply with e.d.
2500m
which is the same as(decimal)2500
. I something alike would have solved your issue...Convert.ToDecimal - https://msdn.microsoft.com/en-us/library/system.convert.todecimal(v=vs.110).aspx
is working on a reply...