Currency validation

You could use a regexp: var regex = /^\d+(?:\.\d{0,2})$/; var numStr = “123.20”; if (regex.test(numStr)) alert(“Number is valid”); If you’re not looking to be as strict with the decimal places you might find it easier to use the unary (+) operator to cast to a number to check it’s validity: var numStr = “123.20”; var … Read more

What is the best way to evaluate mathematical expressions in C++?

Not sure why ‘pow’ only has one parameter, but using the ExprTk library one can derive the following simple solution: #include <cstdio> #include <string> #include “exprtk.hpp” int main() { typedef exprtk::expression<double> expression_t; typedef exprtk::parser<double> parser_t; std::string expression_string = “3 + sqrt(5) + pow(3,2) + log(5)”; expression_t expression; parser_t parser; if (parser.compile(expression_string,expression)) { double result = … Read more

Converting Expression to String

This may not be the best/most efficient method, but it does work. Expression<Func<Product, bool>> exp = (x) => (x.Id > 5 && x.Warranty != false); string expBody = ((LambdaExpression)exp).Body.ToString(); // Gives: ((x.Id > 5) AndAlso (x.Warranty != False)) var paramName = exp.Parameters[0].Name; var paramTypeName = exp.Parameters[0].Type.Name; // You could easily add “OrElse” and others… expBody … Read more

The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly

I know this question already has an accepted answer, but for me, a .NET beginner, there was a simple solution to what I was doing wrong and I thought I’d share. I had been doing this: @Html.HiddenFor(Model.Foo.Bar.ID) What worked for me was changing to this: @Html.HiddenFor(m => m.Foo.Bar.ID) (where “m” is an arbitrary string to … Read more

XPath 1.0 to find if an element’s value is in a list of values

You can check multiple conditions inside the same square brackets: /Location/Addr[State=”TX” or State=”AL” or State=”MA”] Or if you have a really long list, you can create a list of states and use the contains() function. /Location/Addr[contains(‘TX AL MA’, State)] This will work fine for two-letter state abbreviations. If you want to make it more robust … Read more

With C++11, is it undefined behavior to write f(x++), g(x++)?

No, the behavior is defined. To quote C++11 (n3337) [expr.comma/1]: A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded-value expression (Clause [expr]). Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. … Read more