rounding

Why does 0.5 rounded with MidpointRounding.AwayFromZero result in 0?

一曲冷凌霜 提交于 2020-01-13 10:16:09
问题 Why does this result in 0 and not 1? Math.Round(0.5, 0, MidpointRounding.AwayFromZero) Here's an example: http://ideone.com/ayMVO 回答1: Normally, with issues like that, it's because the number cannot be represented exactly in IEEE754 - it's most likely it's being converted to 0.4999999999... or something like that which would round to zero. However, 0.5 (1/2) is exactly representable in IEEE754 so that's not the case here. It's possible that the compiler made a mistake in converting the text

Round a Python list of numbers and maintain their sum

馋奶兔 提交于 2020-01-13 08:48:07
问题 I have a list or an array of decimal numbers in Python. I need to round them to the nearest 2 decimal places as these are monetary amounts. But, I need the overall sum to be maintained, i.e. the sum of the original array rounded to 2 decimal places must be equal to the sum of the rounded elements of the array. Here's my code so far: myOriginalList = [27226.94982, 193.0595233, 1764.3094, 12625.8607, 26714.67907, 18970.35388, 12725.41407, 23589.93271, 27948.40386, 23767.83261, 12449.81318]

How can I strip zeros and decimal points off of decimal strings?

故事扮演 提交于 2020-01-13 03:57:13
问题 The following code currently outputs: 12.1 12.100 12.1000 12.00 12 12.0000 How can I change it so it outputs: 12.1 12.1 12.1 12 12 12 Math.Round seems to be the thing, but it makes me define how many decimal places I want, but I want them to be variable as above. If there is no mathematical way to do it, I'll just strip the zeros and decimal points off the right side of the strings, but would think there is a math way to handle this. using System; using System.Collections.Generic; namespace

Find the highest number in a set to be rounded down, and round it up instead

自古美人都是妖i 提交于 2020-01-13 02:17:34
问题 As the title describes, I have a set of objects - call them Allocations - which contain a description & a number. All numbers in the set add up to 100%, but for display purpose I sometimes round to a whole percent. In some edge cases, having rounded the numbers I end up with 99%. Example: Description | Actual | Rounded =============================== Allocation A | 65.23% | 65% Allocation B | 25.40% | 25% Allocation C | 7.95% | 8% Allocation D | 1.42% | 1% ===============================

PHP enter percentage number round to 2 decimal places not working

折月煮酒 提交于 2020-01-11 13:12:13
问题 php number round to 2 decimal places not working my number is 0.00000000000000000000001 and I want result as 0.01 . I tried php number_format() function php round() function but they didn't work for me. 回答1: Just make a simple function that returns either the rounded value or the minimum value you would like... function round_special($x) { if ($x == 0) return 0; $rounded = round($x, 2); $minValue = 0.01; if ($rounded < $minValue) { return number_format($minValue, 2); } else { return number

C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?

怎甘沉沦 提交于 2020-01-11 12:18:33
问题 Helo everyone. int[] ai1=new int[2] { 3514,3515 }; void average1() { List<int> aveList = new List<int> { ai1[0],ai1[1]}; double AveragLI = aveList.Average(); int AverLI = (int)Math.Round((double)AveragLI); label1.Text = AverLI.ToString(); } Returns 3514; should not be 3515? 回答1: Math.Round is the culprit int AverLI = (int)Math.Round((double)AveragLI); Its what we call Banker's Rounding or even rounding. Info on Math.Round says The integer nearest a. If the fractional component of a is halfway

Round money to nearest 10 dollars in Javascript

早过忘川 提交于 2020-01-10 17:12:30
问题 How can I round a decimal number in Javascript to the nearest 10? My math is pretty rubbish today, it could be the 2 hour sleep :/ Some sample cases $2823.66 = $2820 $142.11 = $140 $9.49 = $10 I understand I probably need a combination of Math.round/floor but I can't seem to get expected result. Any help/pointers appreciated! M 回答1: Try Math.round(val / 10) * 10; 回答2: Use this function: function roundTen(number) { return Math.round(number/10)*10; } alert(roundTen(2823.66)); 回答3: To round a

Rounding time in Python

情到浓时终转凉″ 提交于 2020-01-10 07:49:10
问题 What would be an elegant, efficient and Pythonic way to perform a h/m/s rounding operation on time related types in Python with control over the rounding resolution? My guess is that it would require a time modulo operation. Illustrative examples: 20:11:13 % (10 seconds) => (3 seconds) 20:11:13 % (10 minutes) => (1 minutes and 13 seconds) Relevant time related types I can think of: datetime.datetime \ datetime.time struct_time 回答1: For a datetime.datetime rounding, see this function: https:/

Round a double to 3 significant figures

徘徊边缘 提交于 2020-01-09 10:36:07
问题 Does anybody know how I can round a double value to 3 significant figures like the examples on this website http://www.purplemath.com/modules/rounding2.htm 回答1: double d = ...; BigDecimal bd = new BigDecimal(d); bd = bd.round(new MathContext(3)); double rounded = bd.doubleValue(); 回答2: public String toSignificantFiguresString(BigDecimal bd, int significantFigures ){ String test = String.format("%."+significantFigures+"G", bd); if (test.contains("E+")){ test = String.format(Locale.US, "%.0f",

using jquery, how would i find the closest match in an array, to a specified number

穿精又带淫゛_ 提交于 2020-01-09 05:08:27
问题 using jquery, how would i find the closest match in an array, to a specified number For example, you've got an array like this: 1, 3, 8, 10, 13, ... What number is closest to 4? 4 would return 3 2 would return 3 5 would return 3 6 would return 8 ive seen this done in many different languages, but not in jquery, is this possible to do simply 回答1: You can use the jQuery.each method to loop the array, other than that it's just plain Javascript. Something like: var theArray = [ 1, 3, 8, 10, 13 ];