I hate decimal numbers. For 1.005 I don't get the result I expect with the following code.

```
#!/usr/bin/perl -w
use strict;
use POSIX qw(floor);
my $num = (1.005 * 100) + 0.5;
print $num . "\n"; # 101
print floor($num) . "\n"; # 100
print int($num) . "\n"; # 100
```

For 2.005 and 3.005 it works fine.

With this ugly "hack" I get the expected result.

```
#!/usr/bin/perl -w
use strict;
use POSIX qw(floor);
my $num = (1.005 * 100) + 0.5;
$num = "$num";
print $num . "\n"; # 101
print floor($num) . "\n"; # 101
print int($num) . "\n"; # 101
```

What is the correct way to do this?

`floor()`

is not for rounding, it goes down to the nearest integer.

See this old post: How do you round a floating point number in Perl?

The “correct” way to round is whatever way **you** choose to define as correct for whatever purpose you have in mind. People have thought a lot about it, and some strategies are more appropriate for some application areas than they are in others.

Rounding toward zero by using `int()`

is seldom what people want. Usually they want something that is numerically unbiased.

Fair splits the fives; hence the shorthand term, “round towards even”. The nearer integer isn’t defined: there is no nearer integer when the *least siginficant digit* is 5. There are 9 things that when rounded give a different answer: 1,2,3,4,5,6,7,8,9.

To be fair, you must have half of those go one way and half go the other. But there are nine numbers, so you have four go one way and four go the other, but now you have a fairness problem. The only way therefore to avoid bias is for the 5 cases to alternate up and down. That’s why it works this way.

```
% perl -e 'printf "%.0f\n", $_+.5 for -10 .. +10'
```

produces the sequence

```
-10 -8 -8 -6 -6 -4 -4 -2 -2 -0 0 2 2 4 4 6 6 8 8 10 10
```

While this round-towards-zero approach:

```
% perl -e 'print int($_+.5)," " for -10 .. +10; print "\n"'
```

makes this with a hump in the middle:

```
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 0 1 2 3 4 5 6 7 8 9 10
```

Looking again at round-towards-even:

```
% perl -le 'printf "%.1f ", $_+.05 for -10 .. +10'
-9.9 -8.9 -8.0 -7.0 -6.0 -5.0 -4.0 -3.0 -1.9 -0.9 0.1 1.1 2.0 3.0 4.0 5.0 6.0 7.0 8.1 9.1 10.1
```

That makes more sense if you do this:

```
% perl -le 'printf "%.2f ", $_+.05 for -10 .. +10'
-9.95 -8.95 -7.95 -6.95 -5.95 -4.95 -3.95 -2.95 -1.95 -0.95 0.05 1.05 2.05 3.05 4.05 5.05 6.05 7.05 8.05 9.05 10.05
```

and then consider what towards even means.

Similar Questions

I want to round number in python but it keep giving me the inaccurate result for example I want to round 99.999999946 into 99.99, or 56.3633333 into 56.36 this is what I've tried: int(99.999999946*100

I have an issue while inserting a double value into a database table with some decimal precision. I am using MS SQL Server version 10.0.4000 Below is a sample table with decimal column having 4 decima

Given that Decimal.MaxValue = 79228162514264337593543950335m Why does the next line give me 7922816251426433759354395034M in the Local window instead of 7922816251426433759354395033.5m as expected? De

I have following jQuery function to make textbox accepting only decimal number, but problem is it didn't accept . , so it works as only integer textbox, I don't want to use any plugin here is the jQue

Forgive me if I'm too noob about this. Recently, I post a question regarding the rounding off two decimal places. Now, How can I get the sum of these numbers but I only need the two decimals w/out rou

I have rounded the 'legInches' but how do I round this to 2 decimal points instead of none? specifically - (int)round(legInches) result = (isTitle)? @Leg Span : [NSString stringWithFormat:@%dcm / %

I need to display a floating point number with a specified number of decimal places (rounding up), specifically up to two decimal places even though the number has no fractional part. One way that I k

Possible Duplicate: C# Double - ToString() formatting with two decimal places but no rounding I'm using float numbers and I want to get the number of decimal points without any rounding-off being pe

How can I get the decimal value of any result? I have looked at Output Format for Numbers, and think that the N function should do the trick, but I can't work it out. For instance: In[1]:= N[Ln[2], 4]

I'm doing simple math in JavaScript using variables to represent the numbers. Here is an example of my code: var ones = 0; var fives = 0; function results (){ _fives = (fives * 5); var res = (_fives +

In C#, I'm trying to get the last two decimal places of a double with NO rounding. I've tried everything from Math.Floor to Math.Truncate and nothing is working. Samples of the results I'd like: 1,424

What is the general way to convert numbers among hex, oct and binary in OCaml? For example, how to convert hex 4294967276 (FFFFFFFC) to decimal number? Thanks! Nick

Could some one help me to find a good way to round decimal monetary values to nearest multiples of 10 cents/pennies 10.47 is 10.50 10.4366677 is 10.40 10.489999 is 10.50 I have my money value stored i

I've got a bunch of numbers that have multiple decimal points (so they're really strings). However, I want to sort them as if they were numbers. 1.1.1 10.2.3 2.6.7 21.10.4 3.10.12 4.11.5 4.1.16 6.4.

Situation: A column containing an number is imported into SSIS as a string. We are converting the string to either an integer or decimal number based on what the data type is expected to be using the

If variable A = 0.07 when echoed and variable B = 2100.00 when echoed if we: $c = $a * $b; when the end result is a whole # ( i.e. $c = 147) is there any way to force the result to keep 2 decimal plac

In Firebird DB: I read about the difference between numeric and decimal but still not sure Which is more suitable for prices calculations: decimal or numeric without rounding numbers ? Thanks

I have an customer invoice table in my MySQL database with a DECIMAL(10,2) field called price. When fetching these values in php and calculating a sum amount, ex: in the script $totalAmount = 0; //

I am a beginner in programmin in general and R specifically. I would like to generate a set of random numbers in a normal distribution but to limit the decimal places in these numbers to only 2. I hav

I found this MDN Decimal Rounding example really neat. It does almost exactly what I want it to do. Almost, because when I do: Math.round10((6.9544 + 6.95001), -2); The output is displayed as 13.9, r

I want to fetch the whole and decimal numbers from paragraph. I'm using jQuery and regular expression for this. here is my code: jQuery(#Content_Text p, #Content_Text td).html(function (index, value

When converting an NSString, which contains standard decimal numbers with two digits (e.g. 8.20) to a NSNumber, I get (from time to time) extra digits and a strange rounding behavior when logging the

I am trying to truncate decimal numbers to decimal places. Something like this: 5.467 -> 5.46 985.943 -> 985.94 toFixed(2) does just about the right thing but it rounds off the value. I don't n

I have a problem with submitting forms when decimal numbers are present, to be precise if there is a number behind the decimal separator it wont submit. I am working with Asp.NET MVC3, Kendo UI and a

I have the following script to calculate inventory values. However, it is rounding to the nearest whole integer without decimal as opposed to two decimal places. The output is formatted correct, but I

This question already has an answer here: Python rounding error with float numbers 2 answers Python weird addition bug [duplicate] 4 answers I'm trying to add decimal numbers a decimal numb

I am using SSRS to write a report and I am running into a very strange issue. I have numbers that are rounding that I do not want to round. Below are the expressions that are in play. I have formatted

I need C code for rounding up a double value to the next greatest integer value. For example, if I have: 1.0 (double) it must be 1 (int) 1.01 (double) it must be 2 (int) 5.67 (double) it must be 6 (i

This is the php I have written which is slightly mixed with some wordpress functions. However, I want all the values to be rounded to one decimal place, but I cannot work out how to do it - the php be

I read all rounding functions of T-SQL like Round,Floor,Ceil but none of them has round down decimal numbers correctly I have 2 questions: how to round down a decimal number? A value of 3.69 must sho

how do i get the numbers after a decimal point? for example if i have 5.55 how do i get .55?

sympy.solve() seems to give me a wrong result. There are known problems with inequalities, e.g. http://code.google.com/p/sympy/issues/detail?id=3244 but this is simple enough that it should work: imp

I understand that floating point numbers can often include rounding errors. When you take the floor or ceiling of a float (or double) in order to convert it to an integer, will the resultant value be

Possible Duplicate: Truncate (not round) decimal places in SQL Server Can't figure this out. I need 1 decimal place returned while SQL is rounding to whole numbers. I read that integers divided by

I would like to be able to limit the amount of decimal places that are shown from a print function without any sort of rounding system v = 8.836333333333339 print ('%.2f' % v) This code will print the

Well I am having trouble to print numbers with decimal point. When I run python and ask it to do a division, in which the result must be a number with decimal point, it doesn't print the decimal point

I am re-writing a COBOL program in C# that does calculations dealing with money. From some of the first calculations however, some of the decimal rounding is off (doesn't match what the COBOL program

I have written a program in perl.My requirement is to print the only the decimal numbers, not exponential numbers. Could you please let me know how to implement this ? My program is calculating the ex

Possible Duplicate: Math.Round not keeping the trailing zero How to round double values but keep trailing zeros I have a decimal that I want to round to 2 dp. So, I have used Math.Round(). When I hav

I want to round up or down a until the result is an integer. a = 1.2 b = 1.5 a*b*16 = 28.8 However, I have a criterion: a must be fixed to no more than 3 decimal places. So I need a calculation where

I am trying to sum my decimal records e.g. There is a column in my table named cash having values like 40.33, 30.00, 77.22. How can I create a query that sum up these records? I am trying hard using S

In bash I am trying to code a conditional with numbers that are decimals (with fractions). Then I found out that I cannot do decimals in bash. The script that I have is as follows: a=$(awk '/average

I'm trying to design a script that given any arbitrary command-line input in euros and cents, calculate the change, using the minimum number of notes or coins. The script works fine with numbers with

I wanted to know is it possible format a double to round to two decimal places with trailing zeros without using DecimalFormat, or importing anything for that matter? I've been trying to think of one

I am trying to subtract two numbers and get a result of decimal number with two decimal places, but when the subtraction is happening, I am getting full number, without any decimals. Here is what I am

I want to print floats in a good looking way. Specifically I want to print two numbers after the decimal point, but only if these numbers are not zero. This works if the number is not an even integer:

To round a number I use the following code: public static roundBd(BigDecimal bd){ BigDecimal result1 = bd.setScale(0, RoundingMode.HALF_UP); return result1; } Input 1.50 --> Output 2 Input 1.499

How can I restrict input to a text-box so that it accepts only numbers and the decimal point?

I am launching a query (Access 2003) from VB.NET app. select id, customer, date, total, iif ([amountGet] - [amountSent] = [total], 'Yes', 'No') as result from invoices but iif returns wrong results TA

Bit of an odd question. I'm using cocoa. I have a series of numbers e.g.: 0.87 0.32 1.12 2.34 8.82 12.66 and I want to get the bit before the decimal place, with no rounding. Like this: 0.87 ->