## Rounding float to int

Self studying coding (noob here), the answer to a practice problem is as follows: amount = (int) round (c); Where c is a float. Is it safe to say that this line converts the float to an integer through rounding? I tried researching methods of conv...
more »

## How to round all the values of a prop.table in R in one line?

In R 3.4.0, how can I round all the percentages generated by prop.table() to 3 decimal places in one line? This does not work: MyTable\$MyCol %>% table() %>% prop.table()*100 %>% round(.,3) -7 -2 0 1 ...
more »

## How to round to specific values in Python

I am working on an algorithm to automatically create character sheets for a roleplaying game. In the game, you have attributes which you put points into to increase them. However, at a certain value it takes 2 points to increase the value of the actu...
more »

## Pandas Shift Converts Ints to Float AND Rounds

When shifting column of integers, I know how to fix my column when Pandas automatically converts the integers to floats because of the presence of a NaN. I basically use the method described here. However, if the shift introduces a NaN thereby conve...
more »

## Rounding elements in numpy array to the number of the same index in another array

For example: a=[[ 2.22323422 3.34342 ] [ 24.324 97.56464 ]] round_to= [[2 1] [1 3]] My expected output would be: a_rounded= [[ 2.2 3. ] [ 2. 97.6]] I would like to do this without slicing out each elem...
more »

## I do *not* want correct rounding for function exp

The GCC implementation of the C mathematical library on Debian systems has apparently an (IEEE 754-2008)-compliant implementation of the function exp, implying that rounding shall always be correct: (from Wikipedia) The IEEE floating point standa...
more »

## Different values for these two rounding expressions?

SELECT ROUND(152.7300, 2, 1), --returns 152.7300 ROUND('152.7300', 2, 1) --returns 152.72 Why does the second rounding expression result in downward rounding to .72 ? I imagine it has something to do with the VARCHAR input being converted to a nu...
more »

## Associativity of floating-point multiplication in a special case

It's well known that IEEE floating-point multiplication is not associative. However, consider the special case where a, b, and c are 32-bit signed integers (in C): double da = (double) a; double db = (double) b; double dc = (double) c; Now, does ...
more »

## Select and average values in a vector which are too close to each other

I have an ordered vector, such as: c(2, 2.8, 2.9, 3.3, 3.5, 4.7, 5.5, 7.2, 7.3, 8.7, 8.7, 10) I want to not only remove duplicates (which is easy with unique()), but also to average values which are too close to each other, based on a closeness th...
more »

## Issue with non-integer node positions after loading scene from a file in Xcode

I have had a strange issue which I thought I would share. Although a work-arounds are fairly simple, they are a bit tedious, and I was wondering whether there was a better way. I designed a SpriteKit scene using the scene editor, loaded the scene an...
more »

## Nice way to truncate an integer

I have a function which is given a buffer which accepts to be filled up to a size_t length; however, the actual call which fills it wants an int as max length. So, in case the parameter cannot fit in an integer, I want it truncated to the maximum v...
more »

## Reliable JS rounding numbers with toFixed(2) of a 3 decimal number

I am simply trying to round up 1.275.toFixed(2) and I was expecting a return of 1.28, rather than 1.27. Using various calculators and the simple method of rounding to the nearest hundredth, if the last digit is greater than or equal to five, it shou...
more »

## Python 3 code - Rounding integers

I am trying to round numbers up in python 3. In my existing code, the number either round up to the nearest 10 or down. For example, 67 goes to 70 and 64 goes to 60. I would like the number to always round up to the nearest multiple of 10, so that 67...
more »

## Round off to next 0.05 in SQL Server

I have a requirement, where I need the rates to be rounded off to nearest nickel. For e.g: Rate . . . . Rounded Value 1.90 . . . . 1.90 1.91 . . . . 1.95 1.92 . . . . 1.95 1.93 . . . . 1.95 1.94 . . . . 1.95 1.95 . . . . 1.9...
more »

## SQL Server numbers rounding issue

This is a follow-up question to [TSQL number rounding issue. It's the same code: IF OBJECT_ID(N'dbo.rounding_testing') IS NOT NULL DROP FUNCTION dbo.rounding_testing; GO CREATE FUNCTION dbo.rounding_testing ( @value FLOAT, @digit INT ) R...
more »

## Convert to double value to integer without rounding off

I give view height according to percentage of screen. But views only get integer value. How can I convert integer values without rounding the numbers. I calculate according to the screen. As the double values turn integer values, the roundings are bl...
more »

## Julia: Optimize a cost function with `Optim.jl` and `autodiff` for integers

I like to optimize (minimize) the following given function (quad_function) by using Optim.jl with automatic differentiation (autodiff=true). My objective function rounds Real values to whole numbers and is therefore step-like. As I use the autodiff...
more »

## Python 3 Decimal rounding half down with ROUND_HALF_UP context

Can anybody explain or propose a fix for why when I round a decimal in Python 3 with the context set to round half up, it rounds 2.5 to 2, whereas in Python 2 it rounds correctly to 3: Python 3.4.3 and 3.5.2: >>> import decimal >>>...
more »

## Why does C# round (100-1)/2 to 49 instead of 50?

According to my calculator: (100-1) / 2 = 49.5 If I have an int like this: int mid = (100 - 1) / 2 And printing mid will give me: 49 Why will C# give me 49 instead of 50? Aren't you supposed to round to the next whole number if it is .5 so tha...
more »

## Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn't?

I know that most decimals don't have an exact floating point representation (Is floating point math broken?). But I don't see why 4*0.1 is printed nicely as 0.4, but 3*0.1 isn't, when both values actually have ugly decimal representations: >>...
more »

## What does "number | 0" mean in Javascript?

I saw someone use this as a replacement for Math.floor() and was curious what it means and how it works. i.e. instead of doing: answer = Math.floor(number/2); they had: answer = number/2 | 0; Tried to search for it online and within SO but cou...
more »

## Round-off / round-up criteria in Python

I'm porting a MATLAB code to Python 3.5.1 and I found a float round-off issue. In MATLAB, the following number is rounded up to the 6th decimal place: fprintf(1,'%f', -67.6640625); -67.664063 In Python, on the other hand, the following number is ...
more »

## Using round() function in c

I'm a bit confused about the round() function in C. First of all, man says: SYNOPSIS #include <math.h> double round(double x); RETURN VALUE These functions return the rounded integer value. If x is integral, +0, -0, NaN, or infin...
more »

## Why do 4 different languages give 4 different results here?

Consider this (all commands run on an 64bit Arch Linux system): Perl (v5.24.0) \$ perl -le 'print 10190150730169267102/1000%10' 6 awk (GNU Awk 4.1.3) \$ awk 'BEGIN{print 10190150730169267102/1000%10}' 6 R (3.3.1) > (10190150730169267102/1000)...
more »

## Rounding some numbers, and converting some to int

I made a method that takes two numbers and returns a calculated value rounded to three decimals. I'm curious to know how I can have numbers such as 1.141 to be rounded but numbers like 5.0 turned into integers (5). code: def calculateHypotenuse(a,b...
more »

## rounding errors in Python floor division

I know rounding errors happen in floating point arithmetic but can somebody explain the reason for this one: >>> 8.0 / 0.4 # as expected 20.0 >>> floor(8.0 / 0.4) # int works too 20 >>> 8.0 // 0.4 # expecting 20.0 19.0 ...
more »

## java integer rounding (division related)

I have run into a surprise with integer division not rounding down as expected. Simple code: public class HelloMath { public static void main(String[] args) { for (int s=1; s< 20; s++) { int div = 1<<s; ...
more »

## Why are my decimal values being rounded to integers in SQL insertions?

I'm using SQL Server Management Studio and have the following schema in place: CREATE TABLE tmp( id int NOT NULL IDENTITY(1,1)PRIMARY KEY, toleranceRegion DECIMAL ) Then I perform the following insertions: INSERT INTO tmp VALUES(3.2); IN...
more »

## rounding 1 to have two floating digits

''' Your task is to write a function which returns the sum of following series upto nth term(parameter). Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +... ''' Kind of non-trivial but I tried to use round(1,2) to show 1.00 but it shows 1.0, what can ...
more »

## How to round a number to a chosen integer

In Denmark we have an odd grading system that goes as follows. [-3,00,02,4,7,10,12] Our assignment is to take a vector with different decimal numbers, and round it to the nearest valid grade. Here is our code so far. import numpy as np def roundGr...
more »

## Store as integer = rounded number? Stored as string instead, but is this bad?

I would appreciate it if you guys could help me because I want to know the best way to solve a problem I had. I wrote this code (with some help from Stack Overflow), but for Dim ROIrange As Range Dim ROImax As Integer For i = 1 To 30 Set R...
more »