rounding

Built in .Net algorithm to round value to the nearest 10 interval

帅比萌擦擦* 提交于 2019-12-17 04:27:51
问题 How to, in C# round any value to 10 interval? For example, if I have 11, I want it to return 10, if I have 136, then I want it to return 140. I can easily do it by hand return ((int)(number / 10)) * 10; But I am looking for an builtin algorithm to do this job, something like Math.Round(). The reason why I won't want to do by hand is that I don't want to write same or similar piece of code all over my projects, even for something as simple as the above. 回答1: There is no built-in function in

How to round floats to integers while preserving their sum?

孤街醉人 提交于 2019-12-17 04:18:02
问题 Let's say I have an array of floating point numbers, in sorted (let's say ascending) order, whose sum is known to be an integer N . I want to "round" these numbers to integers while leaving their sum unchanged. In other words, I'm looking for an algorithm that converts the array of floating-point numbers (call it fn ) to an array of integers (call it in ) such that: the two arrays have the same length the sum of the array of integers is N the difference between each floating-point number fn[i

How to round floats to integers while preserving their sum?

ε祈祈猫儿з 提交于 2019-12-17 04:17:32
问题 Let's say I have an array of floating point numbers, in sorted (let's say ascending) order, whose sum is known to be an integer N . I want to "round" these numbers to integers while leaving their sum unchanged. In other words, I'm looking for an algorithm that converts the array of floating-point numbers (call it fn ) to an array of integers (call it in ) such that: the two arrays have the same length the sum of the array of integers is N the difference between each floating-point number fn[i

Round up to nearest multiple of five in PHP

夙愿已清 提交于 2019-12-17 04:02:27
问题 I want a php function which returns 55 when calling it with 52. I've tried the round() function: echo round(94, -1); // 90 It returns 90 but I want 95 . Thanks. 回答1: This can be accomplished in a number of ways, depending on your preferred rounding convention: 1. Round to the next multiple of 5, exclude the current number Behaviour: 50 outputs 55, 52 outputs 55 function roundUpToAny($n,$x=5) { return round(($n+$x/2)/$x)*$x; } 2. Round to the nearest multiple of 5, include the current number

Rounding numbers in Objective-C

蹲街弑〆低调 提交于 2019-12-17 02:22:54
问题 I'm trying to do some number rounding and conversion to a string to enhance the output in an Objective-C program. I have a float value that I'd like to round to the nearest .5 and then use it to set the text on a label. For example: 1.4 would be a string of: 1.5 1.2 would be a string of: 1 0.2 would be a string of: 0 I've spent a while looking on Google for an answer but, being a noob with Objective-C, I'm not sure what to search for! So, I'd really appreciate a pointer in the right direction

How can I round up the time to the nearest X minutes?

两盒软妹~` 提交于 2019-12-17 02:22:10
问题 Is there a simple function for rounding UP a DateTime to the nearest 15 minutes? E.g. 2011-08-11 16:59 becomes 2011-08-11 17:00 2011-08-11 17:00 stays as 2011-08-11 17:00 2011-08-11 17:01 becomes 2011-08-11 17:15 回答1: DateTime RoundUp(DateTime dt, TimeSpan d) { return new DateTime((dt.Ticks + d.Ticks - 1) / d.Ticks * d.Ticks, dt.Kind); } Example: var dt1 = RoundUp(DateTime.Parse("2011-08-11 16:59"), TimeSpan.FromMinutes(15)); // dt1 == {11/08/2011 17:00:00} var dt2 = RoundUp(DateTime.Parse(

Should I use NSDecimalNumber to deal with money?

耗尽温柔 提交于 2019-12-17 02:11:50
问题 As I started coding my first app I used NSNumber for money values without thinking twice. Then I thought that maybe c types were enough to deal with my values. Yet, I was advised in the iPhone SDK forum to use NSDecimalNumber, because of its excellent rounding capabilities. Not being a mathematician by temperament, I thought that the mantissa/exponent paradigm might be overkill; still, googlin' around, I realised that most talks about money/currency in cocoa were referred to NSDecimalNumber.

How to round a number to significant figures in Python

≡放荡痞女 提交于 2019-12-16 20:57:12
问题 I need to round a float to be displayed in a UI. E.g, to one significant figure: 1234 -> 1000 0.12 -> 0.1 0.012 -> 0.01 0.062 -> 0.06 6253 -> 6000 1999 -> 2000 Is there a nice way to do this using the Python library, or do I have to write it myself? 回答1: You can use negative numbers to round integers: >>> round(1234, -3) 1000.0 Thus if you need only most significant digit: >>> from math import log10, floor >>> def round_to_1(x): ... return round(x, -int(floor(log10(abs(x))))) ... >>> round_to

Python 3.x rounding behavior

半城伤御伤魂 提交于 2019-12-16 19:32:06
问题 I was just re-reading What’s New In Python 3.0 and it states: The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) and the documentation for round: For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done toward the even

Rounding to the second decimal spot [duplicate]

こ雲淡風輕ζ 提交于 2019-12-14 03:46:05
问题 This question already has answers here : Round a float to a regular grid of predefined points (11 answers) Closed 2 years ago . how do i round to the second decimal point in C++. thanks for your help. 回答1: You can multiply by 100 and then round to an integer. Then put the decimal point after the first 2 digits. For example: void round(double x) { double y = 100 * x; int rounded = (int)(y + 0.5); printf("%lf rounded = %d.%02d\n", x, rounded / 100, rounded % 100); } 回答2: When printing doubles