how can round the number in specific format? [duplicate] - javascript

This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 6 years ago.
I want to round the number in specific format in jquery
For ex.
var First = 3.52
if last value after decimal point 2 is less then 3 then it should be round to 0
if last value after decimal point 2 is beetween then 3 to 7 then it should be round to 5
if last value after decimal point 2 is more then 7 then it should be round to 10
Like
if(var First = 1.21) { var Answer = 1.20 }
if(var First = 1.24) { var Answer = 1.25 }
if(var First = 1.28) { var Answer = 1.30 }
i have tried Round and Ceil , Floor but doesnt work for me

To achieve this you can use Math.round() to the nearest 0.05, like this:
Math.round(number * 20) / 20;
To make it easier you can extract this to a function:
console.log(round(1.21)); // = 1.20
console.log(round(1.24)); // = 1.25
console.log(round(1.28)); // = 1.30
function round(num) {
return (Math.round(num * 20) / 20).toFixed(2);
}
Note the use of toFixed(2) to force the result to 2 decimal places. Beware that this method returns a string, so if you are planning on doing any calculations with the value you will need to run the result through parseFloat().

Related

Limit and round number [duplicate]

This question already has an answer here:
Limit the length of number by rounding it
(1 answer)
Closed 8 months ago.
I want to limit the number to max 5 digits by rounding the decimal. If we have less than 5 digits the number should stay untouched.
Could anyone help me to write that function?
function limitAndRoundNumber(number) {
...
}
Exemplary inputs and outputs:
limitAndRoundNumber(1.234) should return 1.234 (unchanged because number has less than 5 digits)
limitAndRoundNumber(1.234567) should return 1.2346
limitAndRoundNumber(12.34567) should return 12.346
limitAndRoundNumber(123.4567) should return 123.46
limitAndRoundNumber(1234.567) should return 1234.6
limitAndRoundNumber(12345) should return 12345
Input number can be grater than 0 and less than 100000
I tend to use the following to cut a number to a set number of decimals
Math.round(number * multiplier) / multiplier
Where 'multiplier' is 10 to the power of the number of decimals. Now you only need to figure out what the multiplier needs to be, and you can do that by rounding to a whole value and getting the string length of the number. So something like:
function limitAndRoundNumber(number) {
const wholeDigits = Math.round(number).toString().length;
const power = wholeDigits <= 5 ? 5 - wholeDigits : 0;
const multiplier = 10**power;
return Math.round(number * multiplier) / multiplier;
}
console.log(limitAndRoundNumber(1.23));
console.log(limitAndRoundNumber(1.234567));
console.log(limitAndRoundNumber(12.34567));
console.log(limitAndRoundNumber(123.4567));
console.log(limitAndRoundNumber(1234.567));
console.log(limitAndRoundNumber(12345));
console.log(limitAndRoundNumber(123456));
One solution is to convert the number to a string and use String.slice(0,requiredLength) to adjust the length before returning a number version of the string.
The length would depend on whether the number contained a decimal separator, which could be determined by a conditional within the function.
Working snippet:
function limitAndRoundNumber(number) {
const length = (number.toString().indexOf('.')) ? 6 : 5;
return parseFloat(number.toString().slice(0,length));
}
console.log(limitAndRoundNumber(1.234567));
console.log(limitAndRoundNumber(12.34567));
console.log(limitAndRoundNumber(123.4567));
console.log(limitAndRoundNumber(1234.567));
console.log(limitAndRoundNumber(12345));
console.log(limitAndRoundNumber(12));
console.log(limitAndRoundNumber(12.3));
The function could be modified to allow for any length, by including a length argument and referencing it in the ternary operator test for the decimal separator:
length = (number.toString().indexOf('.')) ? length+1 : length;

Negative Numbers Give Error NaN in Javascript [duplicate]

This question already has answers here:
Math.pow with negative numbers and non-integer powers
(2 answers)
Closed 3 years ago.
I am trying to write a code about changing numbers to its cube root but whenever i enter a negative number, i receive NaN error.
Tried to use string to number function called Number
let number = prompt("Enter a number");
if (number > 0 || number < 0){
let x = number**(1/3);
alert(x);
}
else if (number == 0){
alert(number);
}
else{
alert("Error");
}
I enter -8 for example, i expect it will give me -2 as result but i receive NaN
This is because cube-root of a negative number has complex numbers as solutions along with a negative real number, to get the negative real number you have to write a simple logic:
function cubeRoot(number){
const isNegative = number < 0;
const cubeRoot = Math.pow(Math.abs(number), 1/3);
return isNegative ? -cubeRoot : cubeRoot;
}
console.log(cubeRoot(-8));
console.log(cubeRoot(0));
console.log(cubeRoot(8));
There is a library available to do this: math.js
console.log(math.cbrt(-8));
console.log(math.cbrt(0));
console.log(math.cbrt(8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.2.1/math.js"></script>

Round the number with JavaScript [duplicate]

This question already has answers here:
How do I round a number in JavaScript?
(8 answers)
Round number up to the nearest multiple of 3
(13 answers)
Closed 4 years ago.
I want to round the numbers like this:
Value Expected
0,523% 1%
2,235% 2,5%
-0,081% -0,5%
-1,081% -1,5%
How can I do this with JavaScript?
Solution:
static round (num) {
const abs = Math.abs(num);
const sign = num < 0 ? -1 : 1;
return sign * (abs % 1 > 0.5 ? Math.ceil(abs) : Math.floor(abs) + 0.5) }
}
I used Excel for it =round(Value/Granularity)*Granularity but I tried it in JavaScript not working.
static calc(num){
const granularitiy = 0.00005;
let calc = Math.round((num / granularitiy)) * granularitiy;
return calc;
}
My granularity value is 0.00005;
Since on only want to round to .5 or round numbers, you can't simply use Math.round(number).
The easiest way to do this would be to have something like:
Math.round(number*2)/2
This way, you'll alway have a number .5 or round.
If you want to round another way (ceil or floor), you can simply replace the Math.round by whatever you want.

How do I use the '^' in this function in JavaScript? [duplicate]

This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 5 years ago.
Write a function called "computeCompoundInterest".
Given a principal, an interest rate, a compounding frequency, and a
time (in years), "computeCompoundInterest" returns the amount of
compound interest generated.
var output = computeCompoundInterest(1500, .043, 4, 6);
console.log(output); // --> 438.8368221341061
Reference:
https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest
This shows the formula used to calculate the total compound interest generated.
Problem is i am trying to use this formula and can't get it right
function computeCompoundInterest(p, i, compoundingFrequency, timeInYears) {
p = p * (1 + (i/4)))^(compoundingFrequency*timeInYears)
}
I tried to step through each calculation and it looks like once I get to:
p = 1500 * (1 + 0.043/4)^ 4 x 6 //compoundingFrequency = 4 and timeInYears = 6
I am doing something wrong. This website seems to get a decimal number when you (1 + (i/4)))^(compoundingFrequency*timeInYears)
^ operator is XOR operator.
For exponentation you should use function Math.pow (like Math.pow(2,3) === 8) or ** operator (like 2**3 === 8)

Javascript: Comparing two float values [duplicate]

This question already has answers here:
Javascript float comparison
(2 answers)
Closed 6 months ago.
I have this JavaScript function:
Contrl.prototype.EvaluateStatement = function(acVal, cfVal) {
var cv = parseFloat(cfVal).toFixed(2);
var av = parseFloat(acVal).toFixed(2);
if( av < cv) // do some thing
}
When i compare float numbers av=7.00 and cv=12.00 the result of 7.00<12.00 is false!
Any ideas why?
toFixed returns a string, and you are comparing the two resulting strings. Lexically, the 1 in 12 comes before the 7 so 12 < 7.
I guess you want to compare something like:
(Math.round(parseFloat(acVal)*100)/100)
which rounds to two decimals
Compare float numbers with precision:
var precision = 0.001;
if (Math.abs(n1 - n2) <= precision) {
// equal
}
else {
// not equal
}
UPD:
Or, if one of the numbers is precise, compare precision with the relative error
var absoluteError = (Math.abs(nApprox - nExact)),
relativeError = absoluteError / nExact;
return (relativeError <= precision);
The Math.fround() function returns the nearest 32-bit single precision float representation of a Number.
And therefore is one of the best choices to compare 2 floats.
if (Math.fround(1.5) < Math.fround(1.6)) {
console.log('yes')
} else {
console.log('no')
}
>>> yes
// More examples:
console.log(Math.fround(0.9) < Math.fround(1)); >>> true
console.log(Math.fround(1.5) < Math.fround(1.6)); >>> true
console.log(Math.fround(0.005) < Math.fround(0.00006)); >>> false
console.log(Math.fround(0.00000000009) < Math.fround(0.0000000000000009)); >>> false
Comparing floats using short notation, also accepts floats as strings and integers:
var floatOne = 2, floatTwo = '1.456';
Math.floor(floatOne*100) > Math.floor(floatTwo*100)
(!) Note: Comparison happens using integers. What actually happens behind the scenes: 200 > 145
Extend 100 with zero's for more decimal precision. For example use 1000 for 3 decimals precision.
Test:
var floatOne = 2, floatTwo = '1.456';
console.log(Math.floor(floatOne*100), '>', Math.floor(floatTwo*100), '=', Math.floor(floatOne*100) > Math.floor(floatTwo*100));
Comparing of float values is tricky due to long "post dot" tail of the float value stored in the memory. The simplest (and in fact the best) way is: to multiply values, for reducing known amount of post dot digits to zero, and then round the value (to rid of the tail).
Obviously both compared values must be multiplied by the same rate.
F.i.: 1,234 * 1000 gives 1234 - which can be compared very easily. 5,67 can be multiplied by 100, as for reducing the float comparing problem in general, but then it couldn't be compared to the first value (1,234 vel 1234). So in this example it need to be multiplied by 1000.
Then the comparition code could look like (in meta code):
var v1 = 1.234;
var v2 = 5.67;
if (Math.round(v1*1000) < Math.round(v2*1000)) ....

Categories

Resources