I am using the money gem in rails to perform some currency conversion.
I'd like to dynamically set the conversion rate so that I can use it in a script.
currency_code = ":SEK"
conversion_rate = #bank.get_rate(:USD, currency_code).to_f
I get this error:
Money::Currency::UnknownCurrency: Unknown currency ':sek'
Which means it's converting the variable to lower case. If I explicitly put in :SEK I don't have any issues.
I've even tried playing around with this:
cb = "SEK"
conversion_rate = #bank.get_rate(:USD, ":#{cb}").to_f
And
cc = ":SEK"
conversion_rate = #bank.get_rate(:USD, cc.upcase).to_f
However I get the same error.
Any ideas?
bnussey,
I looks like you are passing in your currency as a string instead of a symbol. Try this instead:
currency_code = :SEK
If you need to store a string in the database, Ruby can easily convert it to a symbol.
currency = "SEK"
currency_code = currency.to_sym
=> :SEK
Related
I have an Excel (XLSX) file with a column containing values in different currencies, e.g. "CAD 4711.00", "NOK 56.78", "CHF 123.45".
Now I try to read data from these cells and I just cannot get the currencies. The best I can do is get the value (4711, 56.78, 123.45) but I also need to figure out which currency the cell is in. How can I do this?
I would be relatively happy if I could just get the formatted value but I do not see a way to do that either.
maybe this helps to get started :
$fileExcel = 'mony.xlsx';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($fileExcel);
$sheet = $spreadsheet->getActiveSheet();
$lsValue = $sheet->getCell('A1')->getValue();
$lsFormat = $sheet->getStyle('A1')->getNumberFormat()->getFormatCode();
echo $lsValue .':'. $lsFormat .'<HR>';
the result is something like that:
123:#,##0.00\ "€"
123.12:#,##0.00\ [$EUR]
3123:#,##0.00\ [$₽-444]
split the format and inspect it further for your currency
Hey I am not able to make this conversion correctly. I use below code
RawGUID=17379524724484210731 --It's impossible to store variable this way, it will always convert into test3 eventually. Stored as userdata I cannot change
test1="17379524724484210731"
test2="1.7379524724484e+019"
test3=1.7379524724484e+019
function tohex(num)
local charset = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
local tmp = {}
repeat
table.insert(tmp,1,charset[num%16+1])
num = math.floor(num/16)
until num==0
return table.concat(tmp)
end
RawGUID is an example of what I need to convert into hex string, the rest of the variables is just conversion of the same number.
The code works well for anything that's below 64bits.
To be honest, to achieve my goal I need a string in hex to make the rest of my code work
The result I am getting: F13079A800000000
The result I want: F13079A80000002B
How can I convert the active support time object converted to string back to object, In other words, how can I find the active support object from its string?
Example:
a = ActiveSupport::TimeZone.all.first =
#<ActiveSupport::TimeZone:0x007f8c45bc1848 #name="American Samoa",
#tzinfo=#<TZInfo::TimezoneProxy: Pacific/Pago_Pago>, #utc_offset=nil>
If I convert this object to a string using to_s, I get "(GMT-11:00) American Samoa" .
How can i find the object if i have "(GMT-11:00) American Samoa".
This will eliminate everything between the first pair of parentheses and grab the remaining string:
a = ActiveSupport::TimeZone.all.first.to_s.match(/\(.*?\) (.*)/)[1]
...and with that you can find the ActiveSupport::Timezone object:
ActiveSupport::Timezone[a]
# let
timezone_string = '(GMT-11:00) American Samoa'
# let's capture the "American Samoa" substring from above (as an example)
matches = timezone_string.match /\(GMT.*?\) (.*)/
timezone_name = matches[1]
# then we look up the corresponding Timezone object using the "American Samoa" timezone_name
timezone = ActiveSupport::TimeZone[timezone_name]
Thank you for the answers i did try them and it works.
I also tried this
tz_value = business_timezone.split(')').second.strip
which gives me the name and i am finding the object using
ActiveSupport::TimeZone[tz_value].
I am having hard time coming with the syntax of updating map using cqerl. I have tried the following till now and it doesn't work
statement = "UPDATE keyspace SET data[?] = :data_value WHERE scope = ?;",
values = [{data,"Key Value"},{data_value, "Data Value",{scope, "Scope Value"}]
What am I doing wrong here?
Also setting ttl does not work
statement = "INSERT INTO data(scope)
VALUES(?) USING ttl ?",
values = [{scope, "Scope Value"},{[ttl], 3650}]
Anyone, any idea?
Please note that you are using single quotes around the values, which in Erlang syntax indicates you are using atoms. Based on the documentation cqerl, it doesn't expect atoms there. cqerl data types
For example try:
statement = "INSERT INTO data(scope)
VALUES(?) USING ttl ?",
values = [{scope, "Scope Value"},{[ttl], 3650}]
Based on reply from the contributor on github, it takes an atom, so '[ttl]' is the right way
https://github.com/matehat/cqerl/issues/122
For updating a map correct way is with atom in the values part
statement = "UPDATE keyspace SET data[?] = ? WHERE scope = ?;",
values = [{'key(data)',"Key Value"},{'value(data)', "Data Value",{scope, "Scope Value"}]
I'm attempting to convert a string date into a date that can be stored in the database. These are my attempts:
params[:event][:start_date] = '03-21-2016'
DateTime.strptime(params[:event][:start_date], '%Y-%m-%dT%H:%M:%S%z')
or
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y')
However I keep getting an invalid date error. I'm not sure what I'm doing wrong.
One way to do it would be this:
date_string = '03-21-2016'
month, day, year = date_string.split('-').map(&:to_i)
DateTime.new(year, month, day)
You need to understand what each fragment (eg %Y) in the format string ('%Y-%m-%dT%H:%M:%S%z') means: read this.
http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
Once you know this, you can tailor a format string to the date string you have, or expect to get: in this case, "%m-%d-%Y".
When debugging create a new, basic and simple, version of the code and test that.
require 'date'
params = '03-21-2016'
DateTime.strptime(params, '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
Note the order for the format: '%m-%d-%Y', which works. That's the problem with your first attempt, where you tried to use%Y-%m-%d. There is NO month21or day2016`.
Your second attempt is valid but your question makes it appear it doesn't work. You need to be more careful with your testing:
params = {event:{start_date:'03-21-2016'}}
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>