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 »

2017-04-06 14:04 (1) Answers

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 »

2017-01-04 23:01 (1) Answers

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 »

2016-12-29 10:12 (1) Answers

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 »

2016-12-21 03:12 (1) Answers

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 »

2016-11-16 00:11 (3) Answers

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 »

2016-09-09 23:09 (0) Answers

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 »

2016-08-27 17:08 (1) Answers

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 »

2016-08-25 17:08 (2) Answers

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 »

2016-07-31 17:07 (1) Answers

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 »

2016-07-26 13:07 (5) Answers

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 »

2016-06-21 08:06 (2) Answers

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 »

2016-06-17 11:06 (9) Answers