# Currency conversion function in javascript/jquery

**URL:** <https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923>\
**Category:** Javascript & Ajax\
**Created:** [August 5, 2022, 1:05pm UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923 "2022-08-05T13:05:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 5, 2022, 1:05pm UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/1 "2022-08-05T13:05:02Z")

</div>

Hello,  
This php script converts product currency by currency  
Can we do the same in javascript/jquery

```auto
function convert($cf,$urun,$secilen_paracinsi_id)
{
	// if the product currency is the same as the selected currency, return the product price
	if($product['para_birimi'] == $secilen_paracinsi_id)
	{
		return $urun['b_fiyati'];
	}
	// the currency_unit is different, convert the price
	// if the product currency|selected currency matches a from|to entry in $cf, multiple by the factor
	$index = $urun['para_birimi'] .'|'. $secilen_paracinsi_id;
	if(isset($cf[$index]))
	{
		return $urun['b_fiyati'] * $cf[$index];
	}
	// if the selected currency|product currency matches a from|to entry in $cf, divide by the factor
	$index = $secilen_paracinsi_id .'|'. $urun['para_birimi'];
	if(isset($cf[$index]))
	{
		return $urun['b_fiyati'] / $cf[$index];
	}
}

```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [August 5, 2022, 1:53pm UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/2 "2022-08-05T13:53:00Z")

</div>

Of course you can, using very similar syntax.

It would probably be better if the product/urun array input is broken into separate price and currency\_unit parameters, so that the function call has - the cf array of conversion factors, the input price/b\_fiyati, the input currency\_unit/para\_birimi, and the selected/secilen\_paracinsi\_id currency output.

Are you dynamically building the $cf array from a database query or is it hard-coded?

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [August 5, 2022, 2:51pm UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/3 "2022-08-05T14:51:47Z")

</div>

Here, the only syntax differences are in the . vs + concatenation operator and isset vs in -

```auto
<?php

// $cf is an array containing the conversion factors - the from|to values are used as an index and the cf (conversion factor) is the value
// you would query the conversion database table to build the following array of data
$cf = [];
$cf['USD|TL'] =	5.8313;
$cf['EURO|TL'] = 6.4087;
$cf['USD|EURO'] = .9030;
?>

<script>
// produce javascript object with the cf data
const cf = <?=json_encode($cf)?>

function convert(cf, price, currency_unit, selected_currency)
{
	// if the product currency is the same as the selected currency, return the product price
	if(currency_unit == selected_currency)
	{
		return price;
	}
	// the currency_unit is different, convert the price
	// if the product currency|selected currency matches a from|to entry in cf, multiple by the factor
	var index = currency_unit + '|' + selected_currency;
	if(index in cf)
	{
		return price * cf[index];
	}
	// if the selected currency|product currency matches a from|to entry in cf, divide by the factor
	index = selected_currency + '|' + currency_unit;
	if(index in cf)
	{
		return price / cf[index];
	}
}

// example usage
console.log(convert(cf, 10, 'USD', 'TL').toFixed(2));

console.log(convert(cf, 10, 'EURO', 'TL').toFixed(2));

</script>
```

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 5, 2022, 3:30pm UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/4 "2022-08-05T15:30:41Z")

</div>

> [@phdr](#):
>
> Are you dynamically building the $cf array from a database query or is it hard-coded?

It comes from the database  
This is how I get `$cf['USD|TL'] = 10.000` for use in php codes  
exactly as you wrote 😀 👍  
I am trying

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 5, 2022, 5:04pm UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/5 "2022-08-05T17:04:11Z")

</div>

I added it this way, no problem, right?  
return price.toFixed(2);

```auto
function convert(cf, price, currency_unit, selected_currency)
{
	// if the product currency is the same as the selected currency, return the product price
	if(currency_unit == selected_currency)
	{
		return price.toFixed(2);
	}
	// the currency_unit is different, convert the price
	// if the product currency|selected currency matches a from|to entry in cf, multiple by the factor
	var index = currency_unit + '|' + selected_currency;
	if(index in cf)
	{
		return (price * cf[index]).toFixed(2);
	}
	// if the selected currency|product currency matches a from|to entry in cf, divide by the factor
	index = selected_currency + '|' + currency_unit;
	if(index in cf)
	{
		return (price / cf[index]).toFixed(2);
	}
}

```

When the page is loaded in edit mode I’m getting it inside the variable like below  
Saved data from database  
price = ‘250’;  
currency\_unit = ‘USD’;  
“selected\_currency” returns the selected currency to convert with the select option  
sample TL

```auto
$("#ucret").val(convert(cf, price, currency_unit, newval));

```

Thank you very much again

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 6, 2022, 6:48am UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/6 "2022-08-06T06:48:43Z")

</div>

Hello again,  
It converts the currency but not back to the original currency I wonder where did I go wrong  
[https://jsfiddle.net/ademgenc/zc5aq0ut/14/](https://jsfiddle.net/ademgenc/zc5aq0ut/14/)

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 6, 2022, 7:06am UTC](https://www.phphelp.com/t/currency-conversion-function-in-javascript-jquery/34923/7 "2022-08-06T07:06:35Z")

</div>

If the default currency and the selected currency are the same, set “price \* 1” in the field and it will return to the original currency.  
interesting situation

```
if(currency_unit == selected_currency)
{
	return price * 1;
}
```
