rounding

Rounding to even in C#

▼魔方 西西 提交于 2019-12-21 09:18:03
问题 I'm not seeing the result I expect with Math.Round. return Math.Round(99.96535789, 2, MidpointRounding.ToEven); // returning 99.97 As I understand MidpointRounding.ToEven, the 5 in the thousandths position should cause the output to be 99.96. Is this not the case? I even tried this, but it returned 99.97 as well: return Math.Round(99.96535789 * 100, MidpointRounding.ToEven)/100; What am I missing Thanks! 回答1: You're not actually at the midpoint. MidpointRounding.ToEven indicates that if you

Decimal.Round default setting for MidpointRounding [duplicate]

╄→гoц情女王★ 提交于 2019-12-21 07:08:18
问题 This question already has answers here : Why does .NET use banker's rounding as default? (6 answers) Closed 6 years ago . The following applies: var rounded = Decimal.Round(7.635m, 2); //rounded: 7.63 This, to me, is wrong and unexpected behavior. I would assume the value of rounded to be 7.64 . To achieve this, I can do: var rounded = Decimal.Round(7.635m, 2, MidpointRounding.AwayFromZero); //rounded: 7.64 How can this not be the default behavior of Decimal.Round ? Any good reason for this?

C# How to format a double to one decimal place without rounding

♀尐吖头ヾ 提交于 2019-12-21 06:56:16
问题 I need to format a double value to one decimal place without it rounding. double value = 3.984568438706 string result = ""; What I have tried is: 1) result = value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture) + "%"; // returns 3.98% 2) result = value.ToString("##.#", System.Globalization.CultureInfo.InvariantCulture) + "%"; // returns 4% 3) result = value.ToString("##.0", System.Globalization.CultureInfo.InvariantCulture) + "%"; // returns 4.0% 4) (Following other

What is HALF_EVEN rounding for? [closed]

泄露秘密 提交于 2019-12-21 06:51:34
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I can not imagine a situation that I need to use RoundingMode.HALF_EVEN in Java. What is this rounding mode for? When do I want to use it? Please give me some real world examples. 回答1: It is useful when you are performing multiple rounding operations and want the cumulative result to be a true

cell format round and display 2 decimal places

跟風遠走 提交于 2019-12-21 06:47:06
问题 I have one cell formatted as Number and with 2 decimal places. The actual number is 69.30217 so in my cell, imagine cell A1, it appears like 69.30. This is OK. With that cell, I'm making some concatenations so if I do something like this: "&E5&" The number appears as 69.30217. But if I do this: "&ROUND(E5;2)&" The number appears as 69.3. What can I do to display that zero? What to show 69.30 回答1: Use this &TEXT(E5;"0.00")& 回答2: Another way is to use FIXED function, you can specify the number

Rounding necessary with BigDecimal numbers

时间秒杀一切 提交于 2019-12-21 03:08:12
问题 I want to set scale of two BigDecimal numbers a and b . as in this example : BigDecimal a = new BigDecimal("2.6E-1095"); BigDecimal b = new BigDecimal("2.7E-1105"); int i = 112, j=1; BigDecimal aa = a.setScale(i+j); BigDecimal bb = b.setScale(i+j); and when i run i have this exception: java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divideAndRound(BigDecimal.java:1439) at java.math.BigDecimal.setScale(BigDecimal.java:2394) at java.math.BigDecimal.setScale(BigDecimal

PostgreSQL round(v numeric, s int)

风格不统一 提交于 2019-12-21 02:22:37
问题 Which method does Postgres round(v numeric, s int) use? Round half up Round half down Round half away from zero Round half towards zero Round half to even Round half to odd I'm looking for documentation reference. 回答1: Sorry, I don't see any hint of this in the documentation, but a look at the code indicates it's using round half away from zero ; the carry is always added to digits , thereby increasing the absolute value of the variable, regardless of what its sign is. A simple experiment

Round to the nearest 500, Python

拜拜、爱过 提交于 2019-12-20 10:44:29
问题 I'm looking to find a way to round up to the nearest 500.I've been using: math.ceil(round(8334.00256 + 250, -3)) Whereby I have a value from a scale in a map I am making in ArcGIS. I have the ability to read and write the scale factor (i.e. 1:8334....basically, you set the thousandth and it defaults to a ratio) If the scale factor isn't a factor of 500, I want to round up to the next 500. The math.ceil will round up any decimal value, and the round(n,-3) will round to the nearest thousandth,

C# Math.Round Up

你。 提交于 2019-12-20 07:46:33
问题 I've got a question. I have a decimal and I want to round this at 2 decimals, not at the ordinary way but 0.2013559322033898305084745763 desired result: 0.21 How can I do that? 回答1: It sounds like you want a version of Math.Ceiling , but which takes a number of decimal places. You could just multiply, use Math.Ceiling , then divide again: public static double CeilingWithPlaces(double input, int places) { double scale = Math.Pow(10, places); double multiplied = input * scale; double ceiling =

PHP - number_format and rounding

大城市里の小女人 提交于 2019-12-20 07:22:39
问题 The number_format function in PHP seems to round by default. My understanding is that this function is useful for separating every three numbers with a comma. Like 1403423 becomes 1,404,423 when using number_format. So, if I have a large number that I want rounded to two decimal places, how can I do this and still have the commas properly displayed? Desired behavior: 12042.529 --> 12,042.53 回答1: $a=12042.529; echo number_format($a,2,'.',','); 来源: https://stackoverflow.com/questions/19019409