Truncate a Floating Point Number Without Rounding Up

Truncate number to two decimal places without rounding

Convert the number into a string, match the number up to the second decimal place:

function calc(theform) {    var num = theform.original.value, rounded = theform.rounded    var with2Decimals = num.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]    rounded.value = with2Decimals}
<form onsubmit="return calc(this)">Original number: <input name="original" type="text" onkeyup="calc(form)" onchange="calc(form)" /><br />"Rounded" number: <input name="rounded" type="text" placeholder="readonly" readonly></form>

Truncate f-string float without rounding

I don't think you need f-strings or math functions, if I understand you correctly. Plain old string manipulation should get you there:

a = 0.987654321
print(str(a)[:4])

output:

0.98

Truncate a floating point number without rounding up

Assuming you have a float, try this:

(x * 1000).floor / 1000.0

Result:

1.015

See it working online: ideone

Truncate (not round off) decimal numbers in javascript

upd:

So, after all it turned out, rounding bugs will always haunt you, no matter how hard you try to compensate them. Hence the problem should be attacked by representing numbers exactly in decimal notation.

Number.prototype.toFixedDown = function(digits) {
var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"),
m = this.toString().match(re);
return m ? parseFloat(m[1]) : this.valueOf();
};

[ 5.467.toFixedDown(2),
985.943.toFixedDown(2),
17.56.toFixedDown(2),
(0).toFixedDown(1),
1.11.toFixedDown(1) + 22];

// [5.46, 985.94, 17.56, 0, 23.1]

Old error-prone solution based on compilation of others':

Number.prototype.toFixedDown = function(digits) {
var n = this - Math.pow(10, -digits)/2;
n += n / Math.pow(2, 53); // added 1360765523: 17.56.toFixedDown(2) === "17.56"
return n.toFixed(digits);
}

limited float decimal point without rounding the number

My first thought was to change print("%.3f" % a) to print("%.3f" % (a-0.0005)) but it does not quite work: while it outputs what you want for a=12.341661, if a=12.341 it outputs 12.340, which is obviously not right.

Instead, I suggest doing the flooring explicitly using int():

a = 12.341661
b = int(a*1000)/1000.
print(b)

This outputs what you want:

12.341

To get 3 decimals out even if the input has fewer, you can format the output:

a = 3.1
b = int(a*1000)/1000.
print("%.3f" % b)

Outputs:

3.100

Truncate float (not rounded) to 2 decimal places

This can be done by dropping the extra digits you don't want by using multiplication and division. For example, if you want 0.994 to be 0.99, you can multiply by 100 (to cover 2 decimal places), then truncate the number, and then divide it back by 100 to it to the original decimal place.

example:

  0.994 * 100 = 99.4
99.4 truncated = 99.0
99.0 / 100 = 0.99

So here is a function that will do that:

const truncateByDecimalPlace = (value, numDecimalPlaces) =>
Math.trunc(value * Math.pow(10, numDecimalPlaces)) / Math.pow(10, numDecimalPlaces)

console.log(truncateByDecimalPlace(0.996, 2)) // 0.99

How to truncate the decimal values of a floating point number without rounding?

Use the fix() function:

>> fix(4.55)

= 4

Float number format without rounding in ruby

Unfortunately, unlike Float#round, Float#floor does not accept an amount of digits. The below code implements the desired behaviour.

def floor_float input, digits = 3
input.divmod(10 ** -digits).first / (10 ** digits).to_f
end

This might be used as monkey patch:

class Float
def floor_ext digits = 3
self.divmod(10 ** -digits).first / (10 ** digits).to_f
end
end
22.0098.floor_ext
#⇒ 22.009

Probably more succinct variant as suggested by @Stefan:

class Float
def floor_ext digits = 3
div(10 ** -digits).fdiv(10 ** digits)
end
end
22.0098.floor_ext
#⇒ 22.009

Or, one might deal with strings explicitly:

i, f = 22.0098.to_s.split('.')
#⇒ [ "22", "0098" ]
[i, f[0..2]].join('.')
#⇒ "22.009"


Related Topics



Leave a reply



Submit