rounding

Rounding integers to nearest multiple of 10 [duplicate]

有些话、适合烂在心里 提交于 2019-12-17 09:21:37
问题 This question already has answers here : Returning the nearest multiple value of a number (6 answers) Closed 4 months ago . I am trying to figure out how to round prices - both ways. For example: Round down 43 becomes 40 143 becomes 140 1433 becomes 1430 Round up 43 becomes 50 143 becomes 150 1433 becomes 1440 I have the situation where I have a price range of say: £143 - £193 of which I want to show as: £140 - £200 as it looks a lot cleaner Any ideas on how I can achieve this? 回答1: I would

Discard millisecond part from timestamp

折月煮酒 提交于 2019-12-17 08:45:45
问题 How can I discard/round the millisecond part, better if the second part is also removed from a timestamp w/o timezone ? 回答1: A cast to timestamp(0) or timestamptz(0) rounds to full seconds: SELECT now()::timestamp(0); Fractions are not stored in table columns of this type. date_trunc() truncates (leaves seconds unchanged) - which is often what you really want: SELECT date_trunc('second', now()::timestamp); 回答2: Discard milliseconds: SELECT DATE_TRUNC('second', CURRENT_TIMESTAMP::timestamp);

Extreme numerical values in floating-point precision in R

筅森魡賤 提交于 2019-12-17 07:42:51
问题 Can somebody please explain me the following output. I know that it has something to do with floating point precision, but the order of magnitue (difference 1e308) surprises me. 0: high precision > 1e-324==0 [1] TRUE > 1e-323==0 [1] FALSE 1: very unprecise > 1 - 1e-16 == 1 [1] FALSE > 1 - 1e-17 == 1 [1] TRUE 回答1: R uses IEEE 754 double-precision floating-point numbers. Floating-point numbers are more dense near zero. This is a result of their being designed to compute accurately (the

Extreme numerical values in floating-point precision in R

牧云@^-^@ 提交于 2019-12-17 07:42:07
问题 Can somebody please explain me the following output. I know that it has something to do with floating point precision, but the order of magnitue (difference 1e308) surprises me. 0: high precision > 1e-324==0 [1] TRUE > 1e-323==0 [1] FALSE 1: very unprecise > 1 - 1e-16 == 1 [1] FALSE > 1 - 1e-17 == 1 [1] TRUE 回答1: R uses IEEE 754 double-precision floating-point numbers. Floating-point numbers are more dense near zero. This is a result of their being designed to compute accurately (the

Integer division rounding with negatives in C++

前提是你 提交于 2019-12-17 07:40:35
问题 Suppose a and b are both of type int , and b is nonzero. Consider the result of performing a/b in the following cases: a and b are both nonnegative. a and b are both negative. Exactly one of them is negative. In Case 1 the result is rounded down to the nearest integer. But what does the standard say about Cases 2 and 3? An old draft I found floating on the Internet indicates that it is implementation dependent (yes, even case 2) but the committee is leaning toward making it always 'round

Rounding numbers in R to specified number of digits

不想你离开。 提交于 2019-12-17 07:34:10
问题 I have a problem in rounding numbers in R. I have the following data, and I want to round them to 8 decimal digits. structure(c(9.50863385275955e-05, 4.05702267762077e-06, 2.78921491976249e-05, 8.9107773737659e-05, 5.0672643927135e-06, 5.87776809485182e-05, 2.76421630542694e-05, 5.51662570625727e-05, 2.52790624570593e-05, 2.00407457671806e-05, 8.33373482160056e-05, 7.8297940825207e-05, 2.00407457671806e-05, 8.33373482160056e-05, 7.8297940825207e-05, 2.00407457671806e-05, 8.33373482160056e-05,

Rounding to nearest fraction (half, quarter, etc.)

£可爱£侵袭症+ 提交于 2019-12-17 05:49:09
问题 So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math. Round always up to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90) Round always down to the nearest decimal (1.81 = 1.80, 1.89 = 1.80, 1.85 = 1.80) Round always up to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 2, 1.32 = 1.50) Round always down to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 1.75, 1.32 = 1.25) Round always up to the nearest x.50 / 1 (1.23 = 1

How to Round to the nearest whole number in C#

假装没事ソ 提交于 2019-12-17 05:02:22
问题 How can I round values to nearest integer? For example: 1.1 => 1 1.5 => 2 1.9 => 2 "Math.Ceiling()" is not helping me. Any ideas? 回答1: See the official documentation for more. For example: Basically you give the Math.Round method three parameters. The value you want to round. The number of decimals you want to keep after the value. An optional parameter you can invoke to use AwayFromZero rounding. ( ignored unless rounding is ambiguous, e.g. 1.5 ) Sample code: var roundedA = Math.Round(1.1, 0

How to properly round up half float numbers in Python?

感情迁移 提交于 2019-12-17 05:01:23
问题 I am facing a strange behavior of the round() function: for i in range(1, 15, 2): n = i / 2 print(n, "=>", round(n)) This code prints: 0.5 => 0 1.5 => 2 2.5 => 2 3.5 => 4 4.5 => 4 5.5 => 6 6.5 => 6 I expected the floating values to be always rounded up, but instead, it is rounded to the nearest even number. Why such behavior, and what is the best way to get the correct result? I tried to use the fractions but the result is the same. 回答1: The Numeric Types section documents this behaviour

R round to nearest .5 or .1

会有一股神秘感。 提交于 2019-12-17 05:01:12
问题 I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56) . I am now trying to round to a specific value which is different for each stock. Here are some examples: Current Stock Price Minimum Tick Increment Desired Output 123.45 .50 123.50 155.03 .10 155.00 138.24 .50 138.00 129.94 .10 129.90 ... ... ... I'm not really sure how to do this but am open to suggestions. 回答1: Probably, round(a/b)*b will do the work. > a <- seq(.1,1,.13) > b <- c(.1,.1,.1,.2,.3,