Docs / Learn mapping / Mapper Arithmetic

Calculate decimal values in X12 Mapper

Divide quantities and amounts without losing their fractional part. Use explicit rounding to produce the decimal places required by your partner or output format.

Keep the fractional result

The / operator returns the exact decimal result whenever its decimal expansion terminates. Exact results retain their precision even when they need more than 34 significant digits. The decimal places written in the operands do not limit the result.

println(5/2)                    // 2.5
println(1/8)                    // 0.125
println(toNumber("1e-2")/2)      // 0.005
println(0.1+0.2)                // 0.3

The mapper uses decimal arithmetic. Numeric X12 elements can participate directly in calculations; use toNumber(...) to convert numeric text explicitly.

Repeating results use 34 significant digits

Some quotients, such as 1/3, have an infinite decimal expansion. The mapper rounds these results to 34 significant digits using HALF_UP rounding: round to the nearest value, with ties rounded away from zero.

Significant digits count meaningful digits before and after the decimal point, excluding leading zeros. For example, 123.456 and 0.000123456 both have six significant digits. This rule preserves useful precision for small quantities as well as large amounts; it is not a fixed limit of 34 decimal places.

println(1/3)     // 0.3333333333333333333333333333333333
println(2/3)     // 0.6666666666666666666666666666666667
println(100/3)   // 33.33333333333333333333333333333333

Repeating results remain approximations. Multiplying a rounded quotient back by its divisor does not always recover the original value exactly:

println((1/3)*3)             // 0.9999999999999999999999999999999999
println(round((1/3)*3, 2))   // 1.00

Keep decimal places when adding amounts

Addition preserves the greater number of decimal places from its numeric operands, including when the result is a whole number. Variables, parentheses, and addition assignment follow the same rule:

first=1.00
second=2.00
println(first+second)       // 3.00
println((first+second)*1)   // 3.00
first+=second
println(first)              // 3.00

This preserves formatting without changing numeric equality: 3.00 and 3 are equal as numbers. Use round(...) when the output requires a specific number of decimal places.

Choose the decimal places for an output field

round(number, scale) rounds to the requested number of decimal places using HALF_UP. A scale of 0 rounds to a whole number; a scale of 2 retains two decimal places. Negative scales round to tens, hundreds, and so on.

println(round(1/3, 2))    // 0.33
println(round(5/2, 0))    // 3
println(round(-5/2, 0))   // -3
println(round(5/2, 2))    // 2.50
println(round(1/8, 2))   // 0.13

For example, divide a total amount by the quantity and round the resulting unit price:

total=12.50
quantity=5
unitPrice=round(total/quantity, 2)
toJson({"unitPrice":unitPrice})  // {"unitPrice":2.50}

Round at the point required by the business rule. Rounding each line amount can produce a different total from rounding once after adding all the lines. JSON consumers may display a numeric 2.50 as 2.5; those are the same numeric value.

Check the price basis before extending a line

A line price needs its business context. PO104 is the unit price, and PO105 specifies its basis. For example, 144 units at a price of 73 per hundred (HP) extend to 105.12, not 10512. The quantity unit and any required conversions must also agree with the partner's rules.

The sales-order and printable purchase-order demos treat a blank PO105 as a price per ordered unit. Agree on this convention with your partner. An explicit PE means price per each, so these maps require PO103 to be EA (each). Two dozen at 12.50 per each would extend to 300.00. The demos report E2801 on that line because they do not convert dozens to eaches. They also reject other explicit price bases. Save the unit and price-basis combinations your map supports as mapping tests.

The order maps also use required(...) to check PO102 quantity and PO104 unit price before arithmetic. A missing value reports E2801 with the field and line identifier; a supplied zero remains valid. Valid X12 syntax alone does not guarantee that every business field required by your output is present. Require those fields explicitly instead of defaulting an unknown quantity or price to zero.

Use the same rule with division assignment

value /= divisor uses the same precision rule as value = value / divisor:

ratio=1
ratio/=3
println(ratio)             // 0.3333333333333333333333333333333333
println(round(ratio, 2))   // 0.33

Division by zero is an error for both / and /=. Guard a quantity that may be zero when the mapping has a defined fallback:

total=12.50
quantity=0
unitPrice=quantity==0 ? 0 : round(total/quantity, 2)
print(unitPrice)   // 0

The conditional expression evaluates only the selected branch, so the division is skipped when the quantity is zero.

Check your calculation on a sample

Run the calculation in the Mapper workspace and save representative quantities and amounts as mapping test cases. See Mapper Comparisons for numeric equality and text matching, and the Mapper Built-ins reference for round and toNumber.

Preserve shipment-weight precision

Set the weight in the ASN Params file as a JSON number, for example {"totalWeightInLbs":0.004}. The stylesheet declares this parameter as xs:decimal. Quoting the value makes it a string and the transform reports a type error.

The outgoing ASN examples emit the supplied totalWeightInLbs decimal in TD107 without rounding: 84.125 stays 84.125, and 0.004 stays 0.004. Weight is not an implied-cents amount. Apply a rounding rule only when the partner requires one, then validate the generated X12 field against its length and numeric constraints.

Convert invoice totals to implied cents

The outgoing invoice examples preserve source unit-price precision and convert the reported total once for TDS. They use exact decimal HALF_UP rounding, with ties away from zero: 1.005 becomes TDS*101~, and 12.345 becomes TDS*1235~.

In XSLT, declare xmlns:edi="urn:modernedi:x12" and call edi:cents(totals/amount). In JSLT, call x12-cents(.input.totals.amount). Both helpers return integer-cent text from one decimal number or numeric text value, including signed and scientific notation. Missing, Boolean, collection, and nonnumeric values fail the transform.

For exact amounts supplied through Params, use quoted decimal text, such as {"amount":"1.004999999999999999999"}. Numeric Params are parsed as ordinary JSON numbers before the helper runs and can lose precision even without arithmetic. With that quoted Params value, edi:cents($amount) or x12-cents(.params.amount) returns 100; the unquoted value can become 1.005 and produce 101.

Pass the original source value. JSLT arithmetic and XSLT number() use binary floating point; a cents helper cannot recover precision already lost in a preceding calculation. These helpers do not calculate line extensions, taxes, or discounts. Choose when to round according to the business rule, then check the generated invoice and save boundary cases as mapping tests.