在C#中以特定模式舍入浮点数

时间:2019-06-29 15:34:32

标签: c# unity3d math floating-point rounding

我正在尝试以某种模式将浮点数四舍五入。我希望它只是这些浮点数:

-1,-0.5,0,0.5,1

如何在C#中做到这一点?

(这是我的第一个问题,对不起,对不起)

1 个答案:

答案 0 :(得分:1)

如果x是您的输入:

library(dplyr); library(lubridate)
output <- dat %>%
  mutate(date = ymd_hms(date)) %>%
  mutate(symptom_ep_calc = if_else(symptom > 0, cumsum(symptom > 0 & lag(symptom) == 0), 0L),
         region = cumsum(symptom_ep_calc != lag(symptom_ep_calc, default = 0)))

output %>%
  group_by(region, symptom_ep_calc) %>% summarize(end = max(date)) %>% ungroup() %>%
  mutate(time_recovered = (end - lag(end, default = min(output$date))) / dhours(1),
         recovery_calc = cumsum(time_recovered > 48 & symptom_ep_calc == 0)) %>%
  right_join(output) %>%
  select(date, symptom, symptom_ep_calc, recovery_calc)


# A tibble: 13 x 4
   date                symptom symptom_ep_calc recovery_calc
   <dttm>                <dbl>           <int>         <int>
 1 2015-01-01 06:00:00       0               0             0
 2 2015-01-01 16:53:00       1               1             0
 3 2015-01-02 05:15:00       1               1             0
 4 2015-01-03 05:28:00       1               1             0
 5 2015-01-04 05:13:00       0               0             0
 6 2015-01-05 05:25:00       0               0             0
 7 2015-01-06 05:11:00       1               2             0
 8 2015-01-07 05:25:00       1               2             0
 9 2015-01-08 05:20:00       0               0             1
10 2015-01-09 05:17:00       0               0             1
11 2015-01-09 15:25:00       0               0             1
12 2015-01-10 05:22:00       0               0             1
13 2015-01-11 05:19:00       0               0             1
相关问题