0

I am using third party API, that API provide the response provided the value with currency type,

Sample Input : £200000
Expected Output: £200,000

I tried to change the format, but unable to achieve the output.

var sampleInput = '£200000';
var x = convertCurrencyValue(sampleInput);
document.getElementById('test').innerHTML = x;

function convertCurrencyValue(input) {
  var getValue = input.replace(/&/ig, "&");
  var myValue=parseInt(getValue.replace( /^\D+/g, ''));
  return myValue.toLocaleString();
}
<div id="test"> </div>

Note: The currency is not static, it will change based on input. so need to display the value based on input.

I know i didn't write proper code, Please can anyone provide the solution/suggestions

RSKMR
  • 1,812
  • 5
  • 32
  • 73
  • `myValue` will only even contain an integer due to a) `parseInt` and b) removing any non-numeric characters via `replace`. What exactly did you expect the output to be? – Phil Aug 22 '17 at 05:14
  • Expect output like : £200,000 – RSKMR Aug 22 '17 at 05:15
  • Could the number ever come back as a decimal, eg `&pound;20.50`? – Phil Aug 22 '17 at 05:28
  • yes, because i am using thirdparty api, so possible to come – RSKMR Aug 22 '17 at 05:29
  • This get's significantly more difficult then. Are you wanting to format the number for the user's locale or based on the currency symbol returned by the API? – Phil Aug 22 '17 at 05:38
  • 1
    There is [*Number.prototype.toLocaleString*](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) which allows you to specify the language (aka "locale") and currency symbol where the [*Intl object*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) is supported. Symbols like "£" are ambiguous since more than one country has a currency calld "pound" and use that symbol. Better to use the 3 letter currency code (maybe GBP?). It will also format the number according to the conventions of the language. – RobG Aug 22 '17 at 05:42
  • I expect based on the currency symbol returned by the API – RSKMR Aug 22 '17 at 05:42
  • @RSKMR do you know the range of possible values the API might return for the currency symbol? – Phil Aug 22 '17 at 05:42
  • @Phil - mostly it will come Euro currency - like &pound;200000 or &pound;200000.98; . If unable to provide the solution with decimal means we can remove the decimal value. – RSKMR Aug 22 '17 at 05:45
  • If unable to provide the solution with decimal means we can remove the decimal value. we can display currency with value. – RSKMR Aug 22 '17 at 05:47
  • Why not `num.toLocaleString('en', { style: 'currency', currency: 'GBP' })` which gives `£200,000.00`? – RobG Aug 22 '17 at 05:49
  • That's not what I was asking. In order to properly format the number as a currency string based on the symbol returned by the API, you'll need to be able to convert that into an [ISO 4217](https://www.currency-iso.org/dam/downloads/lists/list_one.xml) code. Alternatively, is there anything in the API that might show what currency code it is, eg "EUR", "GBP", "USD", etc? – Phil Aug 22 '17 at 05:49
  • @RobG OP said the symbol is dynamic – Phil Aug 22 '17 at 05:49
  • @Phil—trivial to map "pound" to "GBP" or whatever. – RobG Aug 22 '17 at 05:50
  • @RobG there are [a lot of codes](https://www.currency-iso.org/dam/downloads/lists/list_one.xml). That's why I asked OP if they knew the range of possible values – Phil Aug 22 '17 at 05:50
  • @Phil—yes, which is point I also made (USD, AUD, CAD, …). If the OP just wants the symbol without the actual currency, then just map to any suitable currency (e.g. "dollar" to USD). – RobG Aug 22 '17 at 05:51
  • @Phil - "EUR", "GBP", "USD" this format will not come. only we can consider like - &pound; , &dollar; – RSKMR Aug 22 '17 at 05:54
  • @RobG Haven't found a way to disable `style:'currency'` from adding the symbol. There is no "none" option for `currencyDisplay` :( – Phil Aug 22 '17 at 05:54
  • This is really a duplicate of [*How to print a number with commas as thousands separators in JavaScript*](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript?s=1|3.8333) with a pound or dollar symbol prefix. – RobG Aug 22 '17 at 06:38

3 Answers3

0

This is how I would do it:

var sampleInput = '&amp;pound;200000';
// you can add more currency equivalent
//here I'd used UTF-8 Decimal value of the currency character.
var curr = {"pound":"&#8356;", "peso": "&#8369"};
//index 1 holds what currency, index 2 holds the amount 
var inputArr = sampleInput.split(";");
var output = curr[inputArr[1]] + parseInt(inputArr[2]).toLocaleString();
div = document.querySelector("#test");
div.innerHTML = output;

I hope this helps.

norrin
  • 171
  • 8
0

I wrote a function for this. You can use this function.

function convertToCurrency(input){
  var nums= input.substring(input.lastIndexOf(';')+1);
  var symbol= input.substring(0,input.lastIndexOf(';')+1)
  .replace('&amp;pound;','£');

  return symbol+(Number(nums)).toLocaleString();
}

Uses

var sampleInput = '&amp;pound;200000';
console.log(convertToCurrency(sampleInput));
0

I'd go with decoding the text twice

var text='&amp;pound;200000';
var elem = document.createElement('textarea');
elem.innerHTML = text;
var decoded1 = elem.value;
elem.innerHTML = decoded1;
var decoded2 = elem.value;

Textarea has this nice feature, that it will decode it's innerhtml into it's value. After running this code, variable decoded2 will contain the corrected string. Well, in my environment anyway. I hope this helps.

diynevala
  • 503
  • 1
  • 4
  • 19