Table of Contents

Values

A value is a terminal token representing a concrete element. This can be:

Integers

They are represented using numbers. Numbers may be presented in decimal, hexadecimal, octal, and binary forms using a prefix:

123456
0xFFFF // hex
0o1777 // octal
0177   // octal, only when UseCStyleOctals option is enabled
0b01010101 // binary

Numbers in decimal format may include a leading sign ('-') character to indicate negative numbers.

Integer numbers are normally evaluated as int or, if the value is too big, as long. When the UseBigNumbers flag in set in ExpressionOptions, BigInteger may be used in expressions and may be returned by certain math operations.

A number may contain number group separators. Comma (',') is the default separator, and when Advanced Options are used, it is possible to use a culture-specific or custom separator.

Additionally, when Advanced Options are used, a number may contain underscores for readability. Underscore support in binary, octal, and hex numbers is built-in, while support in decimals requires a patch in Parlot or a custom branch of Parlot.

Floating point numbers

Use '.' (dot) to separate an interer and a fractional part. When Advanced Options are used, it is possible to use a culture-specific or custom separator and even use two separators (e.g., use both ',' and '.').

Note, that the fractional part (the one after the dot) must be present for the number to be recognized as a floating point one.

Floating point numbers are evaluated either as double or, if the DecimalAsDefault flag is set in ExpressionOptions, as decimal. When the UseBigNumbers flag in set in ExpressionOptions, <xref:ExtendedNumerics.BigDecimal> may be used in expressions and may be returned by certain math operations.

Additionally, when Advanced Options are used, a number may contain a currency symbol or identifier before or after the numeric value. Such a symbol is ignored in calculations. However, currency values are evaluated as decimal regardless of whether decimal is the default type for floating-point numbers.

Common notation

123.456
.123
123.0

Scientific notation

You can use the scientific notation, i.e., insert a letter "e" followed by an integer number to denote a power of ten (10^).

1.22e1
1e2
1e+2
1e+2
1e-2
.1e-2
1e10

Percent

When percent calculation is enabled in Advanced Options, the parser will recognize the '%' character and treat the parsed value as percent. Percent participates in expression evaluation. It can be the result of an operation (e.g., in operations like "5% + 2%"), in which case, an instance of Percent is returned.

DateTime

Must be enclosed between '#' (sharp) characters.

#2008/01/31# // for en-US culture
#08/08/2001 09:30:00# 

By default, NCalc uses current Culture to evaluate DateTime values. When Advanced Options are used, the format of date and time values can be customized to a large extent.

Additionally, it is possible to define dates relative to current moment in a humane form (e.g. #today#, #3 weeks ago# or #in 5 days#) as described in the Advanced Value Formats and Operations topic.

Time

Includes Hours, minutes, and seconds. The value must be enclosed between sharps.

#20:42:00#

When Advanced Options are used, the format of time values can be customized to a large extent.

Additionally, it is possible to define periods in a humane form (e.g. #5 weeks 3 days 28 hours#) as described in the Advanced Value Formats and Operations topic.

Booleans

Booleans can be either true or false.

true

Strings

Any characters between single or double quotes are evaluated as string.

'hello'
greeting("Chers")

You can escape special characters using a backslash("\"). The parser will decode \', \", \\,\0, \a, \b, \f, \n, \r, \t, \v and convert them into the corresponding character. The parser will also decode unicode in hex notation escaped as \u{abcd}, where {abcd} is exactly four (4) hex characters and as \x{abcd}, where {abcd} is one to four characters.

Additionally, the parser will recognize raw strings, i.e., the strings with no escaping in them. Such strings may be enclosed in double quotes with the @ (at) before the opening double quote (@"raw string here") or in back quotes '`' (`raw string here`). Of course, a raw string still may not contain a closing character ( " and ` respectively) in it.

`This will be parsed as is without translating \t into a tab`
@"And this will also be parsed as is without translating \t into a tab"

@"But this string with "quotes" unfortunately won't work"

Chars

If you use AllowCharValues, single quoted strings with one character are interpreted as char

var expression = new Expression("'g'", ExpressionOptions.AllowCharValues);
var result = expression.Evalutate();
Debug.Assert(result); // 'g' -> System.Char

Guid

NCalc also supports Guid, they can be parsed with or without hyphens.

b1548bd5-2556-4d2a-9f47-bb8d421026dd
getUser(78b1941f4e7941c9bef656fad7326538)

Function

A function is made of a name followed by braces, containing optionally any value as arguments.

Abs(1)
doSomething(1, 'dummy')

Please read the functions page for details.

Parameters (variables)

A parameter (variable) is a name that can be optionally contained inside square brackets.

2 + x, 2 + [x]

Please read the parameters page for details.

Lists

Lists are collections of expressions enclosed in parentheses. They are the equivalent of List<LogicalExpression> at CLR. The elements of the list may be separated with a comma (",") or a semicolon (";"). A semicolon is recommended to avoid the possible conflict with a decimal separator or a number group separator.

('Chers'; secretOperation(); 3.14)

When Advanced Options are used, the separator(s) that the parser should use to separate the values in the list is defined via the ArgumentSeparator property, which suports comma, semicolon, colon, and space, as well as their combinations, as separators.