mablakey
Hi,
I have created a page that displays foreign exchange rates with 6 decimal places, such as GBP 1.654321 and I want to display only 4. The code that I’ve added is shown below, but it does not have any effect on the resulting display.
Thanks.
//added to format decimal places
pushtable.onChangingValues = formatValues;
//copied from StockListDemo
//WDX is name of field in schema that requires formatting
function formatValues(item, itemUpdate) {
if (itemUpdate == null) return;
var newValue = itemUpdate.getFormattedValue("WDX");
if (newValue != null) {
var formattedVal = formatDecimal(newValue, 4, true);
itemUpdate.setFormattedValue("WDX",formattedVal);
}
}
// format a decimal number to a fixed number of decimals
function formatDecimal(value, decimals, keepZero) {
var mul = new String("1");
var zero = new String("0");
for (var i = decimals; i > 0; i--) {
mul += zero;
}
value = Math.round(value * mul);
value = value / mul;
var strVal = new String(value);
if (!keepZero) {
return strVal;
}
var nowDecimals = 0;
var dot = strVal.indexOf(".");
if (dot == -1) {
strVal += ".";
} else {
nowDecimals = strVal.length - dot - 1;
}
for (var i = nowDecimals; i < decimals; i++) {
strVal = strVal + zero;
}
return strVal;
}
mablakey
I have just discovered that the code for the decimal formatting is working. However, the code below appears to void the formatting. Any suggestions.
pushtable.onChangingValues = function(itemPos, visualUpdateInfo, itemName) {
function formatHighlites(itemPos, visualUpdateInfo, itemName) {
if (visualUpdateInfo != null) {
var cold = (itemPos % 2 == 1) ? "#eeeeee" : "#ddddee";
visualUpdateInfo.setRowAttribute("lightblue", cold, "backgroundColor");
}
}
Mone
Hi,
if there are no typos in your second message, you don't execute anything in your handler, you just declare a function there and you also miss a }
Moreover in the second post there is no call to the formatDecimal method.
Finally, if the code in both posts is in the same page, you're writing the onChangingValues callback two times, so that the first assignment is overwritten by the second one
Probably your onChangingValues assignment should look like this:
[syntax="JS"]
pushtable.onChangingValues = function(itemPos, visualUpdateInfo, itemName) {
if (visualUpdateInfo == null) {
return;
}
var newValue = visualUpdateInfo.getFormattedValue("WDX");
if (newValue != null) {
var formattedVal = formatDecimal(newValue, 4, true);
visualUpdateInfo.setFormattedValue("WDX",formattedVal);
}
var cold = (itemPos % 2 == 1) ? "#eeeeee" : "#ddddee";
visualUpdateInfo.setRowAttribute("lightblue", cold, "backgroundColor");
}
[/syntax]
mablakey
Hi,
Thank-you for the prompt reply. I realised, through further testing last night, that the second onChangingValue call was overwritting the first. Then I struggled with creating the call that you have provided. This works perfectly and this thread is closed.
Thanks, Mike