Financial calculation

You can perform financial calculations in OmniMark using the BCD data type to represent monetary values and fractional numbers such as tax rates. Unlike floating point numbers, BCD numbers provide accurate fractions for financial calculations.

The following code sample shows basic financial calculations using BCD numbers. Note that literal BCD values must be preceded by the keyword bcd.

  import "ombcd.xmd" unprefixed
  
  process
      local bcd price
      local bcd tax-rate
      local integer quantity
      local bcd total
  
      set price to 19.95
      set tax-rate to 0.07
      set quantity to 28
      set total to price * quantity * (1 + tax-rate)
      output "<$,NNZ.ZZ>" % total

The format string "<$,NNZ.ZZ>" uses the BCD template language to create an output string with a leading "$", commas separating digits into groups of three, and two digits after the decimal point.

The following code shows how to read a decimal number from an HTML form and assign the value to a BCD number:

  import "ombcd.xmd" unprefixed
  include "omcgi.xin"
  
  declare #process-input has unbuffered
  declare #process-output has binary-mode
  
  process
      local stream form-data variable
      local stream cgi-data variable
      local bcd price
      local bcd tax-rate
      local integer quantity
      local bcd total
  
      cgiGetEnv into cgi-data
      cgiGetQuery into form-data
  
      set price to form-data{"price"}
      set tax-rate to 0.07
      set quantity to 28
      set total to price * quantity * (1 + tax-rate)
      output "Content-type: text/html"
          || crlf
          || crlf
          || "<HTML><BODY>"
          || "<H1>Order confirmation</H1>"
          || "Your total comes to: "
          || "<$,NNZ.ZZ>" % total