You can't round with a formula, but you can use this JavaScript code in your DynaForm:
Code: Select allfunction roundToFixed(_float, _digits){
var rounder = Math.pow(10, _digits);
return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}
function roundTotal(price, quantity) {
var total = '';
if (price != '' && quantity != '') {
var total = price * quantity;
total = roundToFixed(total, 2)
}
$("#total").setValue(total);
}
//execute when the "price" or "quantity" changes:
$("#price").setOnchange(function(newVal, oldVal) {
roundTotal(newVal, $("#quantity").getValue());
})
$("#quantity").setOnchange(function(newVal, oldVal) {
roundTotal($("#price").getValue(), newVal);
})
roundTotal($("#price").getValue(), $("#quantity").getValue()); //execute when DynaForm loads
Where you have fields with the IDs "price", "quantity" and "total".
If you want to round inside a grid, then delete your formula and use JavaScript like this:
Code: Select allfunction roundToFixed(_float, _digits){
var rounder = Math.pow(10, _digits);
return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}
function roundTotalInGrid(row, price, quantity) {
var total = '';
if (price != '' && quantity != '') {
var total = price * quantity;
total = roundToFixed(total, 2)
}
//where the "total" field is the 4th field in the grid:
$("#orderList").setValue(total, row, 4);
}
//executed when any field changes in the DynaForm
$("form").setOnchange(function(fieldId, newVal, oldVal) {
//check whether the changed field is in the grid:
//replace "orderList" with the name of the grid and "price" and "quantity" with the ID of grid fields
var aMatch = fieldId.match(/^\[orderList\]\[(\d+)\]\[(price|quantity)\]$/);
if (aMatch != null) {
var rowNo = aMatch[1];
if (aMatch[2] == "price") {
var price = newVal;
//where the "quantity" field is the 3rd grid field:
var quantity = $("#orderList").getValue(rowNo, 3);
}
else {
//where the "price" field is the 2rd grid field:
var price = $("#orderList").getValue(rowNo, 2);
var quantity = newVal;
}
roundTotalInGrid(rowNo, price, quantity);
})
You will need to replace "orderList", "price", "quantity" with the IDs of the fields in your DynaForm and the numbers of the grid column in grid.getValue().