Reputation: 11
I'm working in R where I import data from a database. The database provides numbers with floating point rounding errors. For example, 2 values I'm working with are Limit1 <- 0.0280000008642673 and Limit2 <- 0.259999990463257, for example. The database doesn't know any better, I can't force it to give me better data. It's not my database and there's nothing I can do about it there.
Limit1 should be rounded to 3 decimals, and Limit2 to 2 decimals. While I can certainly use the round function, I'm hoping there is an existing function or maybe something someone has written to intelligently convert these to the proper decimals without me having to round them 1 by 1, manually, hardcoded.
I've been dreaming up a kloogy way to count consecutive 9's or 0's and find out the number of significant decimal digits from that information, assuming that none of the true values contain "000" or "999". However, I feel like I can't be the only person who has run into this. There has to be a better way.
Thanks
Upvotes: 1
Views: 498
Reputation: 132706
Based on your clarification in the comments you need something like this:
Limits <- c(0.0280000008642673, 0.259999990463257)
plot(1)
legend("topright", legend = prettyNum(Limits), fill = 1:2)
I really don't see a reason to change the actual numbers if you are only concerned about formatting.
Upvotes: 1