Round down numeric values with decimals

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Round down numeric values with decimals

Nelly Reduan
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Round down numeric values with decimals

Bert Gunter-2
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Round down numeric values with decimals

Bert Gunter-2
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 <[hidden email]> 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 <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

A small thing that amused my small mind

Bert Gunter-2
A few days ago, someone asked how to truncate arbitrary numerics to 1
digit towards 0. e.g. -189 should become -100 and 254 should become
200; and all values in magniude < 1 should become 0.

I proposed a somewhat clumsy solution using floor() and ceiling(), but
in fooling with it a bit more, I realized that a better way to do it
is to use R's trunc() function. It is simpler and works for all cases
AFAICS. Here's a litte function to do it -- maybe someone else might
find it amusing/instructive:

poof <- function(x){
   ## truncating to 0
## x is a numeric vector
k <- 10^trunc(log10(abs(x)))
ifelse(k, trunc(x/k)*k, k)
}

## test it
> x <- c(0,-.036578, .4876, -189, 254)
> poof(x)
[1]    0    0    0 -100  200

Cheers,
Bert

______________________________________________
[hidden email] 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.