Ruby round integer with different length [closed] - ruby

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I can have different-length integer's...
For example 1896, or 894...
But how can i convert them to float, so that i have only one symbol after comma?
For example
1896 -> 1.9
894 -> 0.9
539 -> 0.5
How can i do this on ruby?
Now i do this so:
type.TYP_CCM.round(-2).to_s[0].concat(".").concat(type.TYP_CCM.round(-2).to_s[1])
But it is bad idea, and is only for 4-digit int...

What about that:
(1896/1000.0).round(1) # 1.9
(894/1000.0).round(1) # 0.9
(539/1000.0).round(1) # 0.5

It's all in the API:
(1234/1000.0).round(1)
should give you one decimal digit after the dot.
Even if it were not in the API, you could easily emulate this via
(1234/100.0).round() / 10.0
or more close to your code:
(1234).round(-2) / 1000.0
As for ensuring that your output has the format xxxx.y - use format strings, http://www.ruby-doc.org/core-1.9.3/String.html#method-i-25

Related

How to convert specific format array into hash [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have an array:
ar = ["p=abhZRAh7Un", "a=2", "c=1", "l=3033102", "r=1", "rt=mr", "pid=136330865", "pdid=AiOIhH2vzMPqvhYkxXOxeA%3D%3D", "lks=54.0311", "fks=54.0311"]
and need to convert it into a hash with keys p, a, c, etc. and values – whatever is to the right of the equal sign. What is an elegant way to do that in ruby?
Hash[ar.map{|s| s.split("=")}]
require 'cgi'
ar = ["p=abhZRAh7Un", "a=2", "c=1", "l=3033102", "r=1", "rt=mr", "pid=136330865", "pdid=AiOIhH2vzMPqvhYkxXOxeA%3D%3D", "lks=54.0311", "fks=54.0311"]
CGI.parse(ar.join('&'))
outputs:
=> {"rt"=>["mr"], "fks"=>["54.0311"], "pid"=>["136330865"], "lks"=>["54.0311"], "pdid"=>["AiOIhH2vzMPqvhYkxXOxeA=="], "r"=>["1"], "l"=>["3033102"], "c"=>["1"], "a"=>["2"], "p"=>["abhZRAh7Un"]}

Octave : strange error when using ones [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I don't know why I have this strange error when using Octave on Windows 7 64 bit :
when I use ones(100:100) to declare a matrix 100x100. No problem. But when I use : ones(10:100) I will have error :
memory exhausted or requested size too large
And if I use ones(10:15) I must wait for long time (about 30 seconds) to have answer.
Please explain for me this error.
Thanks :)
Your syntax is wrong. To create an m x n matrix it should be:
ones(m,n)
e.g.
ones(100,100)
or
ones(10,15)

How to get a percentage from a range [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a basic math question. I am trying to get a percentage from a range of numbers and the algorithm is troubling me.
Say I have a range of -5 to +5 and I want to know what percentage is in between given a value. I know that -5 would be equivalent to 0% and 5 would be 100%, with 0 being 50%.
I tried to add 5 to bring up the scale, but it just feels like a hack. I would like it to feel dynamic so that I can give it any range and successfully work.
Ex.
percent = (5 + value) * 100 / 10
How do I figure out what value should be the a general case?
range = top_value - bottom_value
percent = (value - bottom_value) / range

Comparing multiple values in an if statement - Ruby [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Can you compare multiple values in a single if statement in Ruby? If so, how? I know in other languages, you would just use a double ampersand (&&), but I have never seen any example or documentation about it, even in the official ruby documentation! Thanks for all the help in advance.
Yes, you can use the && operator
if a == 1 && b == 2
# do things
end
It IS in the documentation.
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UG

Difference between ASN and ASCII [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Please explain the difference between the ASN and ASCII formats.
What are they used for?
See ASN.1 and ASCII at Wikipedia, the two are orthogonal concepts, one is a notation for data structures the other is a character encoding scheme.

Resources