I'm just started to learn rust and have problem with conversion from string to f32.
In nightly 0.11 was function "from_str" and i use it like this:
let f = std::f32::from_str("0.11", 10);
In current 1.0.0 alpha function not exists.
How to convert from str to f32 now?
you can use parse, now.
let f = "0.11".parse::<f32>(); // returns a Result<f32, std::num::ParseFloatError>`
Related
im trying to make a script to be able to calculate a vector of numbers and math symbols to convert back to a single string
im using eval() function and example would be
str = '4*2'
eval(str)
and the result would be
ans =
8
but when i create into a vector and convert back using horzcat the result won't work.
Number = [52 42 50]
Number1 = (mat2str(char(Number)))
str = horzcat(Number1)
eval(str)
and i would get
ans =
4*2
can anyone help me find the problem with the script?
Your problem is with the mat2str command. This is unecesarry. The char command already returns a string. You end up with a string in a string, so when you eval in your code, you just display the inner string.
First - You should not use eval!
Second, there is no need in horzcat and mat2str in your code. Just write:
str = char(Number)
New to Scala and see people are using sign f ahead of a string, here is an example I tried which works. Wondering what is the function of sign f? Does it need to be combined to use with %s? Tried to search some tutorials but failed. Thanks.
object HelloWorld {
def main(args: Array[String]) {
var start = "Monday";
var end = "Friday";
var palindrome = "Dot saw I was Tod";
println(f"date >= $start%s and date <= $end%s" + palindrome);
// output date >= Monday and date <= FridayDot saw I was Tod
}
}
http://docs.scala-lang.org/overviews/core/string-interpolation.html
The f Interpolator
Prepending f to any string literal allows the creation of simple
formatted strings, similar to printf in other languages. When using
the f interpolator, all variable references should be followed by a
printf-style format string, like %d.
PS. another somewhat related feature is http://docs.scala-lang.org/overviews/quasiquotes/expression-details
See the explanation here. For people coming from C the f interpolator is a printf style formatter. % is to denote the type of data and with a $ you may may refer to a previously defined variable.
The % in not mandatory. Its just that you will get a format that is decided by the compiler at compile time. Bit uyou may want to change the output format sometimes.
So if i take an example ,
var start = "Monday";
var end = "Friday";
val age = 33
var palindrome = "Dot saw I was Tod";
println(f"date >= $start and date <= $end and age<= $age%f" + palindrome);
I could omit the %f and i will see a output of 33 as it will inferred as Int. However i could use %f if i wanted to format it as a float. Also if you use a incompatible formatted you will receive a error at compile time.
In Matlab, I need to format a latex string containing a numeric variable.
The string is like: foo1 , where 1 is contained in variable X and must be subscript.
This line works if I write directly the value of variable
str = texlabel('foo_{1}')
I'm wondering how to insert the X instead of the value.
In fact this line
str = texlabel('foo_{X}')'
produce, of course, fooX
Thanks
The quickest method would be to include a call to sprintf:
X = 1;
str = texlabel(sprintf('foo_{%u}', X));
Which returns:
str =
{foo}_{{2}}
Which we can plot real quick with text(0.1, 0.1, str):
I have a fairly simple question: I have a cell vector that looks like this:
temp_y_date{1} = '2012Q2'
temp_y_date{2} = '2012Q1'
temp_y_date{3} = '2011Q4'
I would like to transform this cell vector into a date vector using the function datenum. I initialy transform the vector to the format 'QQ-YYYY' as follows:
for i = 1:length(temp_y_date)
temp = temp_y_date(i);
year = cellfun(#(c) {c(1:4)}, temp);
quarter = cellfun(#(c) {c(5:6)}, temp);
temp_y_date(i) = strcat(quarter,'-',year);
end
The values of temp_y_date are now
temp_y_date (1) = 'Q2-2012'
temp_y_date (2) = 'Q1-2012'
temp_y_date (3) = 'Q4-2011'
I thought I could now apply the datenum function:
temp_y_date = datenum(temp_y_date,'QQ-YYYY');
However, I get the error:
??? Error using ==> datenum at 178
DATENUM failed.
Caused by:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
I tested on both R2009b and R2012a. Your code works in the latter, but I get the same error in R2009b.
When I dropped the Q in temp_y_date it was fixed in the older version. So apparently older versions don't accept the a quarter definition.
Working code:
strdate = {'2012Q2', '2012Q1', '2011Q4'};
strdate2 = cellfun(#(c) strcat(c(6),'-',c(1:4)),strdate,'uni',false);
result = datenum(strdate2,'QQ-YYYY');
I changed you're variable names, so it's easier for debugging. Also fixed the loop with cellfun in it, you were using it quite inefficiently. I suggest you learn what a cell is, and how to index it.
Note: this will break if you upgrade to another version, or if someone else uses it in a later version. So if you don't want that, I suggest you use verLessThan to distinguish between different versions of matlab:
EDIT:
From help datenum:
Formats with 'Q' are not accepted by DATENUM.
So I guess you'll have to either upgrade or implement this one yourself, doesn't look that hard though:
strdate = {'2012Q2', '2012Q1', '2011Q4'};
if verLessThan('matlab','7.12') % or maybe 7.11, it depends on which version the datenum functionality changed...
strdate2 = cellfun(#(c) sprintf('%s/%02d/01',c(1:4),3*str2double(c(6))-2),strdate,'uni',false);
result = datenum(strdate2,'YYYY/mm/dd');
else
strdate2 = cellfun(#(c) strcat(c(5:6),'-',c(1:4)),strdate,'uni',false);
result = datenum(strdate2,'QQ-YYYY');
end
So as you can see, older versions of datenum need full date spec: 'YYYY/mm/dd'.
I am building a JSON object that is sent in a POST request.
This object has properties that need to be converted from string type to integer type before sending. How does one do that with coffeescript?
Use the javascript parseInt function.
number = parseInt( stringToParse, 10 );
Reference is here.
Remember, coffeescript is just javascript after it's compiled
You can use the less obvious, more magical, less keyboard-intensive operator +:
+"158"
Javascript's parseInt function will achieve this. Remember to set the radix parameter to prevent confusion and ensure predictable behaviour. (E.g. in Coffeescript)
myNewInt = parseInt("176.67", 10)
There's a few good examples in the MDN resources: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
It hasn't been documented in the official manual yet, but it seems that cast operators works too:
myString = "12323"
myNumber = (Number) myString
I don't recommend using parseInt since it's wrong in one case - which I found :
parseInt('09asdf', 10);
#> return 09 which is not correct at all. It should return NaN
The correct answer should be the one from #Corkscreewe. And there is another:
cleanInt = (x) ->
x = Number(x)
(if x >= 0 then Math.floor(x) else Math.ceil(x))
Learn from https://coderwall.com/p/cbabhg
Based on the link, mentioned in nXqd's answer, you can also implicitly convert a string by multiplying it by 1:
'123' * 1 // 123
It behaves correctly for incorrect input:
'123abc' * 1 // NaN
You can do this also with floats:
'123.456' * 1 // 123.456
This is a simple way for your need. NaN will be returned as expected.
parseInt( "09asdf".match(/^\d+$/)?[0] ? NaN, 10)
stringToConvernt = "$13,452,334.5"
output = Number(stringToConvernt.replace(/[^0-9\.]/g, ''))
console.log(output)
//The output is `13452334.5`.
I'm always using bitwise OR to convert strings into integers.
"99.999" | 0 // returns 99