A practical example that calculates line-item totals for a list of products by capturing quantity and price.
ID: 1218
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
// Rule to find quantity and price, then calculate total.
// Note the explicit Double() to convert the captured numerical
// values as text into double-precision numbers for {@Eval}.
t.FromTo("Qty: {@Number:qty}, Price: {@Number:price}",
"{@Self}, Total: {@Eval: Double(qty) * Double(price)}");
var invoice = """
Item: Book, Qty: 3, Price: 15.00
Item: Pen, Qty: 10, Price: 1.50
""";
Console.WriteLine(t.Transform(invoice));
Item: Book, Qty: 3, Price: 15.00, Total: 45
Item: Pen, Qty: 10, Price: 1.50, Total: 15 using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); // Rule to find quantity and price, then calculate total. // Note the explicit Double() to convert the captured numerical // values as text into double-precision numbers for {@Eval}. t.FromTo("Qty: {@Number:qty}, Price: {@Number:price}", "{@Self}, Total: {@Eval: Double(qty) * Double(price)}"); var invoice = """ Item: Book, Qty: 3, Price: 15.00 Item: Pen, Qty: 10, Price: 1.50 """; Console.WriteLine(t.Transform(invoice));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
// Rule to find quantity and price, then calculate total.
// Note the explicit Double() to convert the captured numerical
// values as text into double-precision numbers for {@Eval}.
t.FromTo("Qty: {@Number:qty}, Price: {@Number:price}",
"{@Self}, Total: {@Eval: Double(qty) * Double(price)}");
auto invoice = R"(Item: Book, Qty: 3, Price: 15.00
Item: Pen, Qty: 10, Price: 1.50)";
cout << t.Transform(invoice) << endl;
}
Item: Book, Qty: 3, Price: 15.00, Total: 45
Item: Pen, Qty: 10, Price: 1.50, Total: 15 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; // Rule to find quantity and price, then calculate total. // Note the explicit Double() to convert the captured numerical // values as text into double-precision numbers for {@Eval}. t.FromTo("Qty: {@Number:qty}, Price: {@Number:price}", "{@Self}, Total: {@Eval: Double(qty) * Double(price)}"); auto invoice = R"(Item: Book, Qty: 3, Price: 15.00 Item: Pen, Qty: 10, Price: 1.50)"; cout << t.Transform(invoice) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
'// Rule to find quantity and price, then calculate total.
'// Note the explicit Double() to convert the captured numerical
'// values as text into double-precision numbers for {@Eval}.
t.FromTo("Qty: {@Number:qty}, Price: {@Number:price}",
"{@Self}, Total: {@Eval: Double(qty) * Double(price)}")
Dim invoice = "Item: Book, Qty: 3, Price: 15.00
Item: Pen, Qty: 10, Price: 1.50"
Console.WriteLine(t.Transform(invoice))
End Sub
End Module
Item: Book, Qty: 3, Price: 15.00, Total: 45
Item: Pen, Qty: 10, Price: 1.50, Total: 15 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() '// Rule to find quantity and price, then calculate total. '// Note the explicit Double() to convert the captured numerical '// values as text into double-precision numbers for {@Eval}. t.FromTo("Qty: {@Number:qty}, Price: {@Number:price}", "{@Self}, Total: {@Eval: Double(qty) * Double(price)}") Dim invoice = "Item: Book, Qty: 3, Price: 15.00 Item: Pen, Qty: 10, Price: 1.50" Console.WriteLine(t.Transform(invoice)) End Sub End Module