How can i change currency format pattern, currency symbol and currency symbol position programmatically in magento 2? - currency

I want to change currency format pattern, currency symbol and currency symbol position programmatically. I found some data in folder vendor\magento\zendframework1\library\Zend\Locale\Data. For example:
<currencyFormats numberSystem="latn">
<currencyFormatLength>
<currencyFormat type="standard">
<pattern>#,##0.00 ¤</pattern>
</currencyFormat>
</currencyFormatLength>
</currencyFormats>
And found some class Magento\Framework\Locale\Currency, who work with price.
But I don't know how to use it in custom module.

Related

using SWITCH() Formula in Google Sheets using hash-map

In my Google Sheet table, I use SWITCH command to convert a cell from coin symbol -> into coin value.
what I currently do:
=SWITCH(D660,"₪","ILS","$","USD","Ft","HUF","€","EUR","лв","BGN","£","EGP")
this will convert cell D660 from coin symbol (₪/$/Ft/€/лв/£) -> into coin name
here's an example of my data:
where in column E I have the equation above.
what I want to do:
I want to a more generic way, where I have a "hash-map" table in my Settings tab which contains the following table:
coin_symbol
coin_name
₪
ILS
$
USD
Ft
HUF
€
EUR
лв
BGN
£
EGP
and now I want the =SWITCH(D660,...) to use the table instead of hard coding inserting the conversion table
.
I'm struggling on this one, I tried stuffs like
=SWITCH(D660,A1:A10,B1:B10) or involving ARRAYFORMULA somewhere but nothing worked.
anyone have a suggestion how to implement that?
You can use VLOOKUP()
In table with the coins symbols and value as you described ( where symbols is column A and name column B)
=VLOOKUP([SYMBOL];[A1:B6];2;FALSE)
Where:
Symbol is the symbol you want to replace ( you can use cell reference );
A1:B6 is the table you want to look in;
2 is the column in wich you take the value;
False as the values are not ordered by a number, just use false.
/!\ depending on your locatioon the ; in my formula might have to be replaced by ,
Here's an example you could use

Extracting specific information from a cell in Google sheets

I am just trying to get the ticker symbol from my cells, And my cells actually are in format of "stockexchange:ticker symbol", for example- NYSEARCA:VTI
How can I refer to the cell with my stock exchange and ticker symbol, such that I only extract the ticker symbol in google sheets?
Note: the ticker symbol is always going to be after the symbol colon :
Assuming you have the data in column A
=index(if(len(A2:A), split(A2:A, ":"),),,2)
or
=ArrayFormula(IF(LEN(A2:A), REGEXEXTRACT(A2:A, "[^:]+$"),))
Note: depending on your locale, you may have to change the comma's to semi colons.

GoogleFinance - Remove USD Symbol

Using google spreadsheets to display currency data, with USD as primary field for formula: =LEFT(V10*GoogleFinance("currency:USDEUR"),5)
I cannot figure out how to remove the the USD Symbol ($) from not show up for the additional currencies. It makes no sense to have the **$**xxx being displayed on non-USD data.
I have tried doing Format -> Numbers but none of those options resolve this issue.
The number formats are not working since the LEFT function is converting the value to text. Does this formula work instead:
=SUBSTITUTE(LEFT(V10*GoogleFinance("currency:USDEUR"),5),"$","")
Highlight the cells you want to change. Go to Format --> Number --> More Formats --> Custom number format. Highlight the "$" and delete it (delete the quotes and dollar sign both). Click apply. I have attached before and after pictures.

Dynamically convert currency

I have a column of values in varying currencies. I want to create a column that automatically detects the currency of a given value and converts that to GPB (or any other given currency).
I have the following formula for querying the exchange rate:
=INDEX(GoogleFinance("eurgbp","price",date(2014,9,15)),2,2)
However this requires that you specify the currency you are converting from. I want this to be dynamic, because my list of currencies varies.
You can return a text string representation of a formatted value with:
=TO_TEXT(A1)
So if you have the value 35 formatted as British Pound Sterling, the formula will return the string:
£35.00
Now you can match that string with a lookup table (probably using REGEXMATCH) to return the string required for GoogleFinance(). The trouble is that the native format of, say, the US dollar, is exactly the same as, say, the Australian dollar. However, you can set your own custom number formats (eg with USD or AUD appended) that will be carried through to the TO_TEXT function.
So you would probably need to describe all your possible currency formats to arrive at a more specific solution.
Sounds like you'll need to come up with some kind of encoding for the currency column values. For example:
Currency
----------
USD, 10254 # $102.54
RMB, 1010 # ¥101.0
You'd then need to parse that column value somehow in order to determine the currency type. Or you could have two columns. One for currency type and the other for currency value:
Type Currency
---- --------
USD 10254 # $102.54
RMB 1010 # ¥101.0
By the way, the currency columns are integers so you don't have to use floating point math which is a nice thing to avoid in my experience.
As AdamL mentioned, you need to check if there are currencies with the same symbols in your list of data.
If not, you can do the following:
Amt Text Amt Symbol
=========================================
(data) =TO_TEXT(A2) =left(B2,FIND(REGEXEXTRACT(B2,"[0-9\.\,]"),B2)-1)
This will get your currency symbol from the column A. The formula is finding the first occurrence of something numeric (including . and ,) and then you just take what is on the left of it to obtain the currency.
Then you need a table for each symbol to its ISO 3 letter code so you can use Google Finance to obtain the FX, something like:
Symbol ISO Ccy
======================================
$ USD
£ GBP
€ EUR
Then you can get the ISO ccy by using VLOOKUP and continue the formulas above, here I did the FX for GBP
Amt Symbol FX Convert Amt
===================================================================================================
(formula above) =IF(D2="GBP",1,INDEX(GoogleFinance(D2&"GBP","price",<SOMEDATE>),2,2)) =E2*A2
I saved an example here.

Number Helper - how to return the currency symbol only

I am usually using the function number_to_currency of ActionView::Base in order to get a number formatted in a specific currency. But I need to get the currency symbol only (to display it next to a textfield.
I did not find a better way than I18n.t(:'number.currency.format.unit'). Any better recommendation?
Thanks,
You can format using the currency symbol but no value, i.e. with a format of just %u:
number_to_currency 1, format: '%u'
#=> "$"
If all you want is the currency symbol, can you just grab the first element of the string?
number_to_currency(39.50)[0] = "$"
Or if you want everything except the currency symbol
number_to_currency(39.50)[1..-1] = "39.50"

Resources