Note also that if you wish to include 0 and negative numbers, and your
intent is to truncate to 1 digit towards 0, then you must of course check
for 0 separately and modify what I suggested for x != 0 to:
k <- floor(log10(abs(x)))
ifelse(x <0, ceiling(x*10^(-k)), floor(x*10^(-k))) *10^k
Note that this is all vectorized, so, e.g. ,
> x<- c(-101.8, 101.8)
> k <- floor(log10(abs(x)))
> ifelse(x <0, ceiling(x*10^(-k)), floor(x*10^(-k))) *10^k
[1] -100 100
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Sep 4, 2018 at 11:08 AM Bert Gunter <bgunter.4567 at gmail.com>
wrote:
> This is *not* "rounding down."
>
> But this should do it I think:
> ## (see ?floor)
>
> x <- 3.896e09
> k <- floor(log10(x))
>
> > floor(x*10^(-k))*10^k
> [1] 3e+09
>
> There may be even slicker ways, but this is as slick as I can muster...
>
> Cheers,
> Bert
>
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip
)
>
>
> On Tue, Sep 4, 2018 at 9:33 AM Nelly Reduan <nell.redu at hotmail.fr>
wrote:
>
>> Hello,
>>
>>
>>
>> How can I round down numeric values with decimals? For example,
>>
>>
>>
>> > signif(3.896037e+09, digits = 1)
>>
>> [1] 4e+09
>>
>>
>>
>> The expected result is 3e+09 (and not 4e+09).
>>
>>
>>
>> > signif(8.68542378e-10, digits = 1)
>>
>> [1] 9e-10
>>
>>
>>
>> The expected result is 8e-10 (and not 9e-10).
>>
>>
>>
>> Thank you very much for your time.
>>
>> Have a nice day
>>
>> Nell
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
[[alternative HTML version deleted]]