Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve decimal values when converting POSIXct to character?

Tags:

r

posixct

> t <- Sys.time()
> ct <- as.character(t)
> t - as.POSIXct(ct)
Time difference of 0.4370408 secs

The above example indicates that precision is lost when converting POSIXct to character.

How to preserve the exact value when converting to character so that when I convert back to POSIXct from character I get the original value?

like image 263
Big Cat Public Safety Act Avatar asked Jun 23 '21 12:06

Big Cat Public Safety Act


People also ask

How to convert decimals to characters?

Re: Converting decimal values to character? y = put (x, 10.4 );*I arbitrarily selected 4 decimal...which means 10.4 format allows for 10 characters, from which 4 are the decimal part; Good luck! Anca. Re: Converting decimal values to character?

How do I convert a character field to a numeric field?

With the EVAL operation, there are three options: If no decimal positions exist in the field, you can use the C runtime function atoll () to convert from character to numeric. If you are on V5R2 or later, you can use the %INT built-in function to do the same thing.

How do I get the decimal value in the RPG toolkit?

If you are on V5R2 or later, you can use the %INT built-in function to do the same thing. If the value contains decimal positions, you need to use the CharToNum () function in the RPG ToolKit; if you are on V5R2 or later, the %DEC built-in function will do a similar job to CharToNum, but it's not as good in all situations.

How do I convert a number to a character in Excel?

You can use the little-known "X" edit code to convert numeric fields to character and retain the leading zeros on the value. Then, when used in conjunction with the EVAL operation or the assignment (equals sign) on /FREE syntax, it converts a number to character and zero fills the result.


Video Answer


2 Answers

You can set the option digits.secs to 6 (its maximum value).

options(digits.secs = 6)

t <- Sys.time()
ct <- as.character(t)
t - as.POSIXct(ct)
Time difference of 0 secs
like image 149
ktiu Avatar answered Oct 01 '22 04:10

ktiu


You can use format with %OS6, to give seconds in digits, but still there will be sometimes a difference:

t <- Sys.time()
ct <- format(t, "%Y-%m-%d %H:%M:%OS6")
t - as.POSIXct(ct)
#Time difference of 0 secs
like image 27
GKi Avatar answered Oct 01 '22 04:10

GKi