Page 1 of 1

Javascript output

Posted: Tue Apr 14, 2015 7:13 am
by Sean B.

Hi,

I am a little stuck on how to format rest output since the new updates to database interaction. How to I include the javascript money formatting as described by Maryna Brodina in this post ( "https://c.getsatisfaction.com/apperyi... )

My code looks like this :

// Database Results //
if(value.constructor == Array)
return;

// Start attorney block //
var amount_attorney = value.amnt_atfees;
var attorneyFeesTotal = value.attorneyFeesTotal;
var attorneyFeesTotal = amount_attorney * 1.14 ;
Apperyio('attorneyFeesTotal').text('R ' + attorneyFeesTotal.toFixed(2));
// End attorney block //

// Start deeds block //
var amount_do_fees = value.amnt_dofees;
var deedsOfficeFees = value.deedsOfficeFees;
var deedsOfficeFees = amount_do_fees ;
Apperyio('deedsOfficeFees').text('R ' + deedsOfficeFees.toFixed(2));
// End deeds block //

// Start misc block //
var bond_misc_fees = value.amnt_misc;
var miscFees = value.miscFees;
var miscFees = bond_misc_fees ;
Apperyio('miscFees').text('R ' + miscFees.toFixed(2));
// End misc block //

var bondRegTotal = Number(Apperyio("bondRegTotal").val());
var bondRegTotal = attorneyFeesTotal + deedsOfficeFees + miscFees ;

Apperyio('bondRegTotal').text('R ' + bondRegTotal.toFixed(2));

This is the code (below) that was noted to format the output...just not sure how to add this now since the database changes...

function formatMoney (n) {
var c = 2, /decimals count/
d = ".",/decimal separator/
t = ",", /thousands separator/
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) 3 ? j % 3 : 0;
return '$' + s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
var formattedValue = formatMoney(value);
element.html(formattedValue);
return formattedValue;

Your assistance would be greatly appreciated.

Kind Regards
Sean


Javascript output

Posted: Wed Apr 15, 2015 6:05 am
by Serhii Kulibaba

Hello Sean,

What money format do you need? What exactly doesn't work?


Javascript output

Posted: Tue May 12, 2015 5:20 am
by Sean B.

Hi Sergiy

I need to format the output values that are read and calculated from the database to be formatted from "R1823456.00" to "R 1 823 456.00"

The currency is ZAR (South African Rand).

Kind Regards
Sean


Javascript output

Posted: Wed May 13, 2015 3:15 am
by Yurii Orishchuk

Hi Sean,

Here is a solution for goal convert "R1823456.00" into this string "R 1 823 456.00":

precode

&#47;&#47;You can use here your source string&#46;
var sourceString = "R1823456&#46;00&quot

var execResult = /(\w)(\d+)\&#46;(\d\d)$/gi&#46;exec(sourceString);

var sourceMiddle = execResult[2];
var middlePart = "&quot
console&#46;log(sourceMiddle);
for(var i = sourceMiddle&#46;length - 1; i >= 0; i -= 3){
var startFrom = (i - 2) >= 0 ? (i - 2) : 0;
var length = (i - 2) >= 0 ? 3 : i + 1;
middlePart = sourceMiddle&#46;substr(startFrom, length) + " " + middlePart;
};

var goalString = execResult[1] + " " + middlePart&#46;substr(0, middlePart&#46;length - 1) + "&#46;" + execResult[3];

console&#46;log("from '" + sourceString + "' converted to '" + goalString + "'");

/code/pre

Regards.