Skip to content

Commit a1aeccb

Browse files
committed
Pretty printing of result
1 parent 2b5197f commit a1aeccb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

exercise-book/src/cpp-interop.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ You should get:
3131

3232
Because there are 5 rows of data and a header row.
3333

34-
2. Use `autocxx` to read `weather.csv` and report average temperature in June:
34+
2. Use `autocxx` to read `weather.csv` and report the average temperature in June, which should be approximately
3535

3636
```console
37-
1.1999999999999995
37+
1.20
3838
```
3939

4040
A full solution is available at `rust-exercises/exercises-solutions/cpp-interop`.

exercise-solutions/cpp-interop/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
let doc = ffi::my_csv::open_csv(&file_name).within_unique_ptr();
1616
let count = doc.GetRowCount();
1717

18-
let sum_of_june_temperatures: i64 = (0..count)
18+
let sum_of_june_temperatures: f64 = (0..count)
1919
.filter_map(|row_index| {
2020
let date: UniquePtr<CxxString> = doc.get_string_cell(0, row_index);
2121
let date: &str = date.to_str().ok()?;
@@ -25,11 +25,11 @@ fn main() {
2525
.filter_map(|row_index| {
2626
let temperature: UniquePtr<CxxString> = doc.get_string_cell(1, row_index);
2727
let temperature: &str = temperature.to_str().ok()?;
28-
temperature.parse::<i64>().ok()
28+
temperature.parse::<f64>().ok()
2929
})
3030
.sum();
3131
// June has 30 days
32-
println!("{:.3}", sum_of_june_temperatures as f64 / 30.0);
32+
println!("{:.2}", sum_of_june_temperatures / 30.0);
3333
}
3434

3535
trait GetStringCell {

0 commit comments

Comments
 (0)