Converting string to float using Arduino [closed] - string

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am trying to convert the string in my code to float but using atof() for a sub string seems not be working. Advise on how to go about it appropriately will be appreciated.
It is for a BME280 sensor using 433MHz transmitter and receiver
void loop{
if(rfrx.recv(buff, &bufflen)){
String rxstr = String((char*)buff);
for (int i=0;i=rxstr.length();i++){
if (rxstr.substring(i,i+1) == ","){
String T = rxstr.substring(0,i);
String P = rxstr.substring(i+1);
String A = rxstr.substring(i+2);
String H = rxstr.substring(i+3);
break;
}
}
}
}

Have figured it out
The .toFloat()
Thanks

Related

strings.Contains in switch-case GoLang [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
Is it possible to use strings.Contains in switch case?
something like:
func function(str string){
switch str {
case "str1":
...
case strings.Contains("test"):
...
default:
...
}
}
Edit: its an example, thats not the real case I need it for. Im filtering many results and I want all results that contains X and I also have cases that I need to fully match.
You can do this:
package main
import (
"fmt"
"strings"
)
func main() {
str := "testString"
switch {
case strings.Contains(str, "test"):
fmt.Println(true)
default:
fmt.Println(false)
}
}
https://go.dev/play/p/_2fMd-3kE-r
switch without argument is like switch true.
Why do you need a switch for this boolean output?
Use the following inbuilt function.
func strings.Contains(s string, substr string) bool

Custom types and their usage [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Hello I have 3 custom types and then type created with these 3 types.
type Name = String
type Age = Int
type Semester = Int
type Student = (Name, Age, Semester)
I need to create function which takes student and returns his name
i have created this but it doesn't work.
getName :: Student -> Name:
getName (name_, age_, semester_) = name_
This works fine:
getName :: Student -> Name
getName (name_, age_, semester_) = name_
There shouldn't be any : or anything else in the end of the signature.
First line contains the signature by itself. Then go the lines with the definition. The type alias is Name, not Name:, so that's what should be used.

How to truncate f64 to 2 decimal places? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to truncate f64 to 2 decimal places?
From
let before = 17.69108280254777;
To
let after = 17.69;
You can't actually get rounding without rounding, but i think a workaround like this can get the job done
fn main() {
let before = 17.69108280254777;
let after = f64::trunc(before * 100.0) / 100.0; // or f32::trunc
}
Outputs:
17.69

Idiomatic way of using StringBuilder in kotlin? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I often write pretty complex toString() methods and this question is always bothering me - which variant is more clear to read. Following examples are simplified, usually there are a lot of conditionals, so single liners are not fit.
1) like in plain java:
val sb = StringBuilder()
sb.append(data)
val string = sb.toString()
2) apply + toString() - not pretty yeah?
val string = StringBuilder().apply {
append(data)
}.toString()
3) run + toString() last statement also is not superb
val string = StringBuilder().run {
append(data)
toString()
}
4) ??
#dyukha answer is 100% best choice:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/build-string.html
It's just
val s = buildString { append(data) }
You could skip the StringBuilder and use Kotlin's built-in String Interpolation:
val string = "$data"
Or if things are more complicated:
val string = "The answer is: $data"
Or, using raw strings:
val string =
"""
{
"name": $name,
"value": $value
}
"""

Why does sorted "tan" != "ant"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to sort the characters in a string by sorting a slice of the bytes in the string (using sort.Slice). The code I'm using gets the right results sometimes but other times produces results I can't make sense of.
package main
import (
"fmt"
"sort"
)
func main() {
for _, s := range []string{"nat", "tan", "ant"} {
b := []byte(s)
sort.Slice(b, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(s, string(b))
}
}
https://play.golang.org/p/bC9QWq7aF3G
I would expect "nat", "tan" and "ant" to all be sorted to "ant", but "tan" is sorted to "atn".
Change your sort.Slice line to:
sort.Slice(b, func(i int, j int) bool { return b[i] < b[j] })
sort.Slice needs your less function to compare values in the slice in order to sort the way you intended. Your bug is that you used s rather than b in your less function.

Resources