How to round off decimal values to the nearest highest digit - rounding

How to round off decimal values to the nearest highest digit?
For an ex:
13.799 => 13.80
13.023 => 13.02
27.8555 => 27.86
Is there any algorithm present on this?

Look at Math.Round(decimal) or the overload which takes a MidpointRounding argument.
Math.Round(value)
Another option is to use decimal.Ceiling(13.023)

In recent versions of XPath, you can round to 2 decimal digits using round(., 2).

as Nouman suggested the Math.Round(value) is the right choise.
In you Needs you could use something like this:
public static void Main()
{
//Array where the Information is stored
double[] values = { 13.799, 13.023, 27.8555 };
//for each value in the array
foreach (double value in values)
//write it to the console like unrounded-value => rounded-value
//specify on how many Digit you want to round
Console.WriteLine("{0} => {1}", value, Math.Round(value, 2));
}
The example displays the following output:
13.799 => 13.80
13.023 => 13.02
27.8555 => 27.86
Additional Note:
The Decimal value of 4.5 rounds to 4 rather than 5, because this overload uses the default ToEven convention.

Related

How can I turn a integral and a fractional part back into a f32?

So I have a number, say 4.2, I can extract the integral and fractional parts like so:
let value = 4.3;
let integral = f32::trunc(value);
let fractional = get_frac(value); // returns '3'
This returns the correct things, but how can I turn this back into a f32? I'm storing these as integers and then only at a later date need to convert them back into a float.
Here is how I'm doing the conversion
Your function get_frac() inherently can't be inverted, since it maps different fractional parts to the same integer value. For both 4.2 and 4.02, the return value of get_frac() is 2. There is no way to tell from just the result which original value you started with.
A better approach would be to decide how many digits you want to include in your integer representation of your floating point number. If you want to include, say, four decimal digits, simply multiply your number by 10,000 and round to an integer. This operation can be approximately inverted by dividing by 10,000 again.
Maybe not the best way, but working, would be to transform to string, then parse it:
let res: f32 = format!("{}.{}", integral, fraction).parse().unwrap();
Playground

Javascript function is mutating parameter with node v14.7.0

Node.js is mutating the number value passed into the test function. Any idea why and how to fix? Thank you!
const val1 = 61368443939717130;
const val2 = 861368443939717130;
const val3 = 161368443939717130;
const test = (v) => {
console.log(v);
}
test(val1);
test(val2);
test(val3);
// Output:
// 61368443939717130
// 861368443939717100
// 161368443939717120
It's because the number 161368443939717130 is bigger than the maximum safe integer that Javascript can represent, which is provided by the constant Number.MAX_SAFE_INTEGER. When working with big numbers you should always check that the values are below the limit, or otherwise you will get unexpected results. See more info here.
Your numbers are out of range for javascript.
The max number javascript can handle is 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion).
It is represented by the MAX_SAFE_INTEGER variable. The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1.
Use BigInt, which has no practical upper limit. But BigInt can’t handle decimals.
This means that if you convert from a Number to a BigInt and backward again, you can lose precision.
console.log(Number.MAX_SAFE_INTEGER)
// 9007199254740991
console.log(Number.MAX_SAFE_INTEGER + 10)
// 9007199254741000
console.log(BigInt(Number.MAX_SAFE_INTEGER) + 10n)
// 9007199254741001n

Provide Decimal Point Number to .toLocaleString() in TypeScript

I am trying to form a number having a decimal separator from a string in order to be able to apply .toLocaleString() method , which cannot be applied on strings.
const initialString = 122; //number
const decimalStr = initialString.toFixed(initialString); //string
const formattedStr = decimalStr.toLocaleString('de-DE');//error,decimalStr is a string
If any conversion is to be applied to the decimalStr, the decimal digits would be lost ( i.e. decimalStr = +decimalStr or decimalStr = Number(decimalStr). => problem, need to keep decimal digits.
How can i keep the decimal points and make the .toLocaleString()
see calling value as a number?
The Number.toLocaleString method accepts a second argument called options.
You can use this argument to control the number of fraction digits. Therefore, your call would be:
const initialNumber = 122;
const formattedStr = initialNumber.toLocaleString('de-DE', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
For more details check here.
Also, notice that both toFixed and the maximumFractionDigits have limits: 100 and 20, respectively. So the example you provide fails on the second line.

How to use Math.round on a big deciaml in groovy?

I have a variable in my groovy script that collects a cost value. However the value comes out as 91.7776766667 and I want it to be rounded up so it is displayed as 91.8. How can I change the code below to do this and I am struggling to implement the Math.round feature:
def testcost = jsongroups.xxx.cost.flatten().collect { new Math.round(BigDecimal (it)) }
log.info testcost
Instead of Math.round() you can use BigDecimal.setScale(scale, roundingMode) on your number directly.
def testcost = jsongroups.options.cost.flatten().collect { new BigDecimal (it).setScale(1, RoundingMode.HALF_UP)}
log.info testcost
HALF_UP will round .01-.04 to .0 and .05-.09 to .1. Read the JavaDoc of RoundingMode for other rounding possibilities, like HALF_EVEN etc.
Groovy language has a round() method which going to round your decimal number to specific decimal place as a parameter.
def decimalValue = (Double)91.7776766667
println "Value upto two decimal places >>"+decimalValue.round(2)
Output:
Value upto two decimal places >>91.78

How to display last 4 numbers in an int?

i have an int field that i want to display its last 4 numbers in jsf page, please advise how to accomplish that.
You can use the modulo operation for that, this is also supported in EL using the modulus operator %.
<p>Last 4 numbers: #{bean.number % 10000}</p>
If you need the last four digits of a number, use the modulus operator:
int someInteger = 23421984;
int last4digits = someInteger % 10000;
// last4digits = 1948;

Resources