rounding

Rounding numbers to a specific resolution

陌路散爱 提交于 2019-12-11 14:26:54
问题 I know many languages have the ability to round to a certain number of decimal places, such as with the Python: >>> print round (123.123, 1) 123.1 >>> print round (123.123, -1) 120.0 But how do we round to an arbitrary resolution that is not a decimal multiple. For example, if I wanted to round a number to the nearest half or third so that: 123.123 rounded to nearest half is 123.0. 456.456 rounded to nearest half is 456.5. 789.789 rounded to nearest half is 790.0. 123.123 rounded to nearest

Round just decimal in Javascript

穿精又带淫゛_ 提交于 2019-12-11 13:36:32
问题 I have a value like that: 20.93 I'd like to round it to 20.90 How can I do that in Javascript ? Thanks! 回答1: Multiply the number by 10, round it and then divide the number by 10: var result = Math.round(20.93 * 10) / 10 回答2: I think this should work: number .toFixed(1); 回答3: var num= 20.93 num = Math.floor(num * 10) / 10; // 20.9 num = Math.ceil(num * 10) / 10; //21 回答4: I take it that you want the trailing zero. None of the answers give you that. It has to be a String to have the trailing

Why Is ToString() Rounding My Double Value?

喜你入骨 提交于 2019-12-11 11:25:04
问题 How do I prevent my double value from being rounded when converting to a string? I have tried both Convert.ToString and ToString() with the same result. For example my double may look something like 77.987654321 , and the two strings conversions convert to to 77.98765 . I need to keep the precision of the value as is. 回答1: By default the .ToString() method of Double returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the "G17

Manually implementing a rounding function in C

余生颓废 提交于 2019-12-11 10:52:50
问题 I have written a C program (which is part of my project) to round off a float value to the given precision specified by the user. The function is something like this float round_offf (float num, int precision) What I have done in this program is convert the float number into a string and then processed it. But is there a way to keep the number as float itself and implement the same. Eg. num = 4.445 prec = 1 result = 4.4 回答1: Of course there is. Very simple: #include <math.h> float custom

get sql server to warn about truncation / rounding

喜夏-厌秋 提交于 2019-12-11 10:46:53
问题 I am working on an ASP.NET C# accounting app and I am in the transition from using floats/doubles to using decimals to represent money. Now I want to control when and where rounding takes place. The way I understand it, SQL server rounds decimal data types differently than C# does. Is there some way to get SQL server (2008) to warn or fail if decimal values (or any kind of data) are getting truncated or rounded? Thanks, 回答1: Look at SET NUMERIC_ROUNDABORT and the table with SET ARITHABORT

MySQL ROUND function using round half down instead of half up

主宰稳场 提交于 2019-12-11 10:09:03
问题 So I'm running MySQL version 5.1.4 and I'm trying to select distinct by a rounded value, and it works great except that it rounds numbers like 12.5 to 12 instead of 13. The columns I have tried this on have all been type double. I did a bit of research and saw the default behavior is to round half up so I'm wondering if there is some setting or parameter I need to change or if this is just broken? Here is my query for reference : SELECT DISTINCT ROUND(radius) as radius FROM arch WHERE radius

Rounding to use for int -> float -> int round trip conversion

最后都变了- 提交于 2019-12-11 09:03:28
问题 I'm writing a set of numeric type conversion functions for a database engine, and I'm concerned about the behavior of converting large integral floating-point values to integer types with greater precision. Take for example converting a 32-bit int to a 32-bit single-precision float. The 23-bit significand of the float yields about 7 decimal digits of precision, so converting any int with more than about 7 digits will result in a loss of precision (which is fine and expected). However, when

rounding a currency

风流意气都作罢 提交于 2019-12-11 07:57:53
问题 i have the following code to round the currency function MyRound(value :currency) : integer; begin if value > 0 then result := Trunc(value + 0.5) else result := Trunc(value - 0.5); end; it worked well so far, my problem now is if i want to round a currency like 999999989000.40 it is giving negative value since Truc takes int and MyRound also returns int. My possible solutions is to convert currency to string and get the string before . and convert the string back to currency. Is this a right

Working with different IEEE floating-point rounding modes in C++

前提是你 提交于 2019-12-11 07:31:15
问题 Woe is me, I have to ensure the same floating-point results on a GPU and on the CPU. Ok, I understand IEEE has taken care of me and provided a nice standard to adhere to with several rounding options; and the CUDA part is sorted out (there are intrinsics for the different rounding modes), so that's just motivation. But in host-side C++ code - how do I perform floating-point arithmetic in a specific rounding mode (and I mean in a specific statement, not throughout my translation unit)? Are

round pandas column with precision but no trailing 0

眉间皱痕 提交于 2019-12-11 07:24:58
问题 Not duplicate because I'm asking about pandas round() . I have a dataframe with some columns with numbers. I run df = df.round(decimals=6) That successfully truncated the long decimals instead of 15.36785699998 correctly writing: 15.367857 , but I still get 1.0 or 16754.0 with a trailing zero. How do I get rid of the trailing zeros in all the columns, once I ran pandas df.round() ? I want to save the dataframe as a csv, and need the data to show the way I wish. 回答1: df = df.round(decimals=6)