You will have to write a java script function that will append , in your value. You can use below java script function -
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
Use below code to override inner HTML of component -
document.getElementById('elementID').innerHTML = addCommas(Value);
The $Component notation is quite picky about the correct number of prefixes, so if your amount, lc1 elements are nested inside other Visualforce components (such as pageblocks, datatables, outputpanels) you need to include the full hierarchy to get the correct name. I used to build this up in baby steps starting with the outermost container and check the page source each time to see if the $Component rendered anything (meaning it was a valid path) or if it was blank (meaning invalid).
These days I use a JQuery selector to find the element that ends with the particular id - that way I don't have to worry about the location prefixes and I can move elements around the page without having to rebuild the "fully qualified" name.
I include JQuery from a CDN, but you could load it as a static resource :
<apex:includescript
value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" />
and then pass the id string literals to the JavaScript function :
<apex:outputLink value="javascript:compute('Amount', 'lc1', ... etc);
Compute
</apex:outputLink>
then build up JQuery selectors based on the literal ids:
<script>
function compute(amount, lc1, lc2, lc3, lc4, lc5, lc6) {
/* Compute the loan charges depending on the loan amount here */
$('[id$=' + lc1 + ']').val(6);
$('[id$=' + lc2 + ']').val(6);
... etc
}
</script>
The key aspect of the selector is the '[id$=' + lc1 + ']' this translates to 'the element with an id that ends with the contents of the lc1 variable'. I've also used the JQuery val() method to set the value of the element.
Best Answer
I compared the output in a spreadsheet vs in JS, and come up with the following table:
I'm not sure what you're considering as descrepency, as the spreadsheet values are less accurate than the JS values by 7 digits past the decimal point, and all of them line up very closely to what the spreadsheet shows.
Perhaps you've made some error in your formula in the spreadsheet, or it may be a bug in Excel (I used Google Spreadsheets to perform this calculation).