How to convert string values like '2 263.32', '592.06' to float numbers (f64) in V-language?
UPD: solved in this way:
'1 456.93'.replace(' ', '').f32() == (float) 1456.93.
Of courcse, this is a workaround and won't work with strings like '$1,592.13'.
Try '592.06'.f64(). I don't think having a whitespace in the number will work currently but you can use underscore instead: '2_263.32'.f64()
Another option is to use strconv module
import strconv
f := strconv.atof64(592.06) ?
Related
I have very large(potentially endless) stream of integers similar to input below.
I intend to randomly access this slice and read from string one character at a time, and would like to access the integer represented by the character.
For the code below I was expecting intVal to be an integer value of 3. number[1] gives me the ASCII code for 3 which is 51.
input := "2345892345234502349502345234534234572304520345902384523045"
intVal,_ := strconv.Atoi(input[1])
Essentially, What is the proper way of reading integers from strings in Go ?
Use the following code to get the numeric value of the decimal number at input[i]:
b := input[i]
if b < '0' || b > '9' {
// not a decimal number
... handle error here
}
n := int(b) - '0'
You can read one rune at a time and convert to string:
for _,r:=range input {
str:=string(r)
}
Or access randomly:
str:=input[n:n+1]
In Google Sheets - I need to sum a set of numbers, where the initial cell contains delimiters and non numerics:
3; 6; 1; 3; None; 1; 1
I first replace all spaces and non numerics:
=REGEXREPLACE(AG24,"\D+",",")
Which gives: 3,6,1,3,1,1
Since =SUM(3,6,1,3,1,1) correctly provides 15, I figured I'd try passing in the REGEXREPLACE result into SUM() and magically have it compute, but doing so yields 0:
=SUM(REGEXREPLACE(AG24,"\D+",",")) = 0
I kind of expected that...
I've also tried SUMPRODUCT, which also yields 0:
=SUMPRODUCT(ARRAYFORMULA(REGEXREPLACE(AG24,"\D+",","))) = 0
Question: so how can I sum the list of string integers?
You can try the below formula which will directly convert the string to array ad then make a sum of it.
=Sum(SPLIT(AG24,";"))
Hope it helps!
Please try:
=sum(split(REGEXREPLACE(AG24,"\D+",","),","))
=SUMPRODUCT(SPLIT(AG24, ";"))
=SUMPRODUCT(SPLIT(REGEXREPLACE(AG24,"\D+"," ")," "))
will work as well...
I am trying to calculate the energy of a photon in (wx)Maxima, using physical_constants and ezunits:
|lambda| : 800 * 10^-9 ` m;
Where | denotes Escape... which displays correctly as a greek lambda, but wxMaxima does not confirm the value as it does usually.
So next I try to use E = h*c/lambda
constvalue (%h * %c / |lambda|) `` J;
But again wxMaxima does not show any result.
Everything works fine if I use lambda spelled in full instead of a greek symbol...
Is the |greek| only good for text inputs?
It's fairly simple: lambda denotes the anonymous lambda function and that one can not be used as a symbol. Use %lambda instead.
A similar thing applies to phi: phi can be used as a symbol, but %phi is a constant with the value 1.61... (golden ratio).
Taking a derivative with respect to %phi instead of phi will for that reason always result in 0 ;-)
Easy question here, probably, but searching did not find a similar question.
The # operator finds the length of a string, among other things, great. But with Lua being dynamically typed, thus no conversion operators, how does one type a number as a string in order to determine its length?
For example suppose I want to print the factorials from 1 to 9 in a formatted table.
i,F = 1,1
while i<10 do
print(i.."! == "..string.rep("0",10-#F)..F)
i=i+1
F=F*i
end
error: attempt to get length of global 'F' (a number value)
why not use tostring(F) to convert F to a string?
Alternatively,
length = math.floor(math.log10(number)+1)
Careful though, this will only work where n > 0!
There are probably a dozen ways to do this. The easy way is to use tostring as Dan mentions. You could also concatenate an empty string, e.g. F_str=""..F to get F_str as a string representation. But since you are trying to output a formatted string, use the string.format method to do all the hard work for you:
i,F = 1,1
while i<10 do
print(string.format("%01d! == %010d", i, F))
i=i+1
F=F*i
end
Isn't while tostring(F).len < 10 do useful?
Working on a rails project where there's an order confirmation string with a credit card number with all but the last four digits starred out. What's the proper way to do a string substitution?
What's the operation to get this
credit_card_number = "1111111111111111"
to this?
credit_card_number = "************1111"
Thanks,
Kenji
Here's a regex approach:
x.gsub!(/.(?=....)/, '*')
Here's an approach using string indexing:
x = '*' * (x.size - 4) + x[-4, 4]
If you're using ActiveMerchant, ActiveMerchant::Billing::CreditCard has an instance method called display_number which does this e.g. XXXX-XXXX-XXXX-4338
If you're not, copy activemerchant:
def last_digits(number)
number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1)
end
def mask(number)
"XXXX-XXXX-XXXX-#{last_digits(number)}"
end
credit_card_number = "1111111111111111"
display_number = mask credit_card_number
You could use Ruby's gsub method and a regular expression to hide some of the numbers in the account number string:
hidenumber = "123-123-1234"
hidenumber.gsub(/(\d{3}-\d{3})/,"xxx-xxx")