rounding

Storing and performing calculations on long decimals in Excel without being rounded

六眼飞鱼酱① 提交于 2020-11-29 19:07:20
问题 I am attempting to do some calculations in Excel on numbers that include long decimals. However, Excel doesn't seem to let me populate the cells with the numbers I would like to use. Example: If I enter 600000.00000000030000000000 into a standard cell on a new spreadsheet, it gets converted to 600000. This makes sense as the cell format is set to General. If I set the cell format to Number and set the decimals places to 20, I would expect to be able to enter the number properly. Instead, the

Rounding half down a decimal

 ̄綄美尐妖づ 提交于 2020-11-29 11:12:40
问题 Does an equivalent of Java RoundingMode.HALF_DOWN exist in C#? For example, I want to round 1.265 to 1.26 , and 1.266 to 1.27 . If not, is there a simple way to do it? 回答1: Have a look at Math.Round e.g. double[] tests = new double[] { 1.265, 1.266, }; var demo = tests .Select(x => $"{x} -> {Math.Round(x, 2, MidpointRounding.AwayFromZero)}"); var report = string.Join(Environment.NewLine, demo); Console.Write(report); Outcome: 1.265 -> 1.26 1.266 -> 1.27 回答2: Use the .Round method with the

Rounding half down a decimal

China☆狼群 提交于 2020-11-29 11:12:27
问题 Does an equivalent of Java RoundingMode.HALF_DOWN exist in C#? For example, I want to round 1.265 to 1.26 , and 1.266 to 1.27 . If not, is there a simple way to do it? 回答1: Have a look at Math.Round e.g. double[] tests = new double[] { 1.265, 1.266, }; var demo = tests .Select(x => $"{x} -> {Math.Round(x, 2, MidpointRounding.AwayFromZero)}"); var report = string.Join(Environment.NewLine, demo); Console.Write(report); Outcome: 1.265 -> 1.26 1.266 -> 1.27 回答2: Use the .Round method with the

Round double values and cast as integers

老子叫甜甜 提交于 2020-11-28 01:56:39
问题 I have a data frame in PySpark like below. import pyspark.sql.functions as func df = sqlContext.createDataFrame( [(0.0, 0.2, 3.45631), (0.4, 1.4, 2.82945), (0.5, 1.9, 7.76261), (0.6, 0.9, 2.76790), (1.2, 1.0, 9.87984)], ["col1", "col2", "col3"]) df.show() +----+----+-------+ |col1|col2| col3| +----+----+-------+ | 0.0| 0.2|3.45631| | 0.4| 1.4|2.82945| | 0.5| 1.9|7.76261| | 0.6| 0.9| 2.7679| | 1.2| 1.0|9.87984| +----+----+-------+ # round 'col3' in a new column: df2 = df.withColumn("col4",

Round double values and cast as integers

|▌冷眼眸甩不掉的悲伤 提交于 2020-11-28 01:56:32
问题 I have a data frame in PySpark like below. import pyspark.sql.functions as func df = sqlContext.createDataFrame( [(0.0, 0.2, 3.45631), (0.4, 1.4, 2.82945), (0.5, 1.9, 7.76261), (0.6, 0.9, 2.76790), (1.2, 1.0, 9.87984)], ["col1", "col2", "col3"]) df.show() +----+----+-------+ |col1|col2| col3| +----+----+-------+ | 0.0| 0.2|3.45631| | 0.4| 1.4|2.82945| | 0.5| 1.9|7.76261| | 0.6| 0.9| 2.7679| | 1.2| 1.0|9.87984| +----+----+-------+ # round 'col3' in a new column: df2 = df.withColumn("col4",

How to round a numpy array?

亡梦爱人 提交于 2020-11-26 04:54:28
问题 I have a numpy array, something like below: data = np.array([ 1.60130719e-01, 9.93827160e-01, 3.63108206e-04]) and I want to round each element to two decimal places. How can I do so? 回答1: Numpy provides two identical methods to do this. Either use np.round(data, 2) or np.around(data, 2) as they are equivalent. See the documentation for more information. Examples: >>> import numpy as np >>> a = np.array([0.015, 0.235, 0.112]) >>> np.round(a, 2) array([0.02, 0.24, 0.11]) >>> np.around(a, 2)