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

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

Related

Converting string to float using Arduino [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 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

How to read two named inegers as a tuple in native Rust? [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 6 months ago.
Improve this question
In Python I can do this:
a, b = map(int, input().split(" "))
... to get exactly 2 integers from user input.
How can I achive the exact same result in Rust (get 2 integers as a named tuple) without any extern crates?
For the sheer fun of it, I translated your code as literally as possible to Rust:
// replace with some read from stdin
let input = "1 2";
let (a, b) = if let &[a, b] = &input.split(' ').map(|c| c.parse::<u32>().unwrap()).collect::<Vec<_>>()[..] {
(a, b)
} else {
panic!("ValueError: too many/few values to unpack (expected 2)");
};
println!("a = {}, b = {}", a, b);

Using value itself without the name of value in Python 3 [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
Is there any way to use variable itself inside without it's name?
For example, I have a string like this:
someStuffVariableName = "abcdefghijklmnop..."
If I want to manipulate it, I need to write every time name of this var but it's so long:
someStuffVariableName = someStuffVariableName[0:-1]
But,anyway,can I do like this:
someStuffVariableName = self[0:-1] or someStuffVariableName = this.value[0:-1]?
There is not.
Your best options are:
Use a more concise variable name (but don't give up readability!)
Just deal with the length
Note that in some cases, the answer is actually yes. For instance, you can often write x += y instead of x = x + y, and x /= y instead of x = x / y.
But this is for assignment operators only.

Getting maximum Key Values in Map [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do I get maximum key values in a Map and let's say save it to a List?
For example, is there is a Map:
John, 30
Alexander, 10
Ivan, 20
Steven, 30
The result must be a List: John, Steven
Without additional List...
Double max = 0d;
for (String key : wagesList.keySet()) {
if (wagesList.get(key) > max) {
max = wagesList.get(key);
}
}
for (String key : wagesList.keySet()) {
if (wagesList.get(key).equals(max)) {
System.out.println(key);
}
}

Haskell: Type to represent a sudoku line? [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 8 years ago.
Improve this question
In Haskell, how can I create a type to represent a list of length 9 which each elements are an Int between 0 and 9?
You could use smart constructors:
module Sudoku(SudokuSquare, sudokuSquare) where
import Data.Traversable(traverse)
data SudokuSquare = SSquare Int
sudokuSquare :: Int -> Maybe SudokuSquare
sudokuSquare i = if i >= 0 && i <= 9 then Just (SSquare i) else Nothing
buildRow :: [Int] -> Maybe [SudokuSquare]
buildRow = traverse sudokuSquare

Resources