I've never looked into regular expressions and unfortunately do not have time - this has to be completed by Tuesday morning BST. What I'm trying to do is simply convert a string into a currency format, to 2 decimal places.
E.g.
1234567.89202304304053050000
=
£1,234,567.89
I could just check the length of the string - how many characters it is - and then just place the commas in the correct places, ensuring only 2 characters are left after the decimal point (I think), but it seems regular expressions or something else (?) would be more efficient?
This is what I have so far, but it only seems to work some times - other times it does not format the string to 2 decimal places:
It was a bit different before but instead of looking for a solid solution, I've just been adding to it to cater times it hasn't worked correctly (i.e. if I saw a number which had been formatted to 2 decimal places, I'd add a bit of code to do so).
I would be extremely grateful if somebody or some people could please help me formulate a solid function which will format a string to GBP currency (up to 2 decimal places), before Tuesday morning BST.
Question
Calum Veteran
I've never looked into regular expressions and unfortunately do not have time - this has to be completed by Tuesday morning BST. What I'm trying to do is simply convert a string into a currency format, to 2 decimal places.
E.g.
1234567.89202304304053050000
=
£1,234,567.89
I could just check the length of the string - how many characters it is - and then just place the commas in the correct places, ensuring only 2 characters are left after the decimal point (I think), but it seems regular expressions or something else (?) would be more efficient?
This is what I have so far, but it only seems to work some times - other times it does not format the string to 2 decimal places:
function FormatCurrency(strNumberToFormat) { if (strNumberToFormat.toString() != "NaN") { if (strNumberToFormat.toString().indexOf(".") != -1) { if ((strNumberToFormat.toString().split('.')[1].substring(0, 1) == strNumberToFormat.toString().split('.')[1].substring(1, 2)) && parseFloat(strNumberToFormat.toString().split('.')[1].substring(1, 2)) < 5) { strNumberToFormat = parseFloat(strNumberToFormat.toString().split('.')[0] + "." + strNumberToFormat.toString().split('.')[1].substring(0, 1) + strNumberToFormat.toString().split('.')[1].substring(0, 1)); } else if ((strNumberToFormat.toString().split('.')[1].substring(0, 1) == strNumberToFormat.toString().split('.')[1].substring(1, 2)) && parseFloat(strNumberToFormat.toString().split('.')[1].substring(1, 2)) >= 5) { strNumberToFormat = parseFloat(strNumberToFormat.toString().split('.')[0] + "." + strNumberToFormat.toString().split('.')[1].substring(0, 1) + (parseFloat(strNumberToFormat.toString().split('.')[1].substring(1, 2)) + 1)); } } strNumberToFormat.toFixed(2); strNumberToFormat += ""; straValue = strNumberToFormat.split("."); strFirstPartOfResult = straValue[0]; strSecondPartOfResult = straValue.length > 1 ? "." + straValue[1] : ""; var objRegex = /(\d+)(\d{3})/; while (objRegex.test(strFirstPartOfResult)) { strFirstPartOfResult = strFirstPartOfResult.replace(objRegex, "$1" + "," + "$2"); } return "£" + strFirstPartOfResult + strSecondPartOfResult; } else { return "£--"; } }It was a bit different before but instead of looking for a solid solution, I've just been adding to it to cater times it hasn't worked correctly (i.e. if I saw a number which had been formatted to 2 decimal places, I'd add a bit of code to do so).
I would be extremely grateful if somebody or some people could please help me formulate a solid function which will format a string to GBP currency (up to 2 decimal places), before Tuesday morning BST.
Thank you in advance,
Calum.
Link to comment
Share on other sites
3 answers to this question
Recommended Posts