How to remove the second digit after decimal point using javascript? - javascript

i want to remove digits after first integer after decimal point using javascript.
if the input is 2.183 then i want the output to be 2.1 how can i do it. i have seen Math.round and
Math.trunc(). but they dont work as needed.
could someone help me with this. thanks.

Use the Math.floor method. By using value 10 here you will get the answer. Note that if you divide the value by 100, you will get the answer as 2.18
let value = 2.183;
value = Math.floor(value * 10) / 10
console.log(value);

Related

How would I round a number (eg: 2.12) to the nearest tenth (2.1) in JS

I am trying to do this but is all I have found is rounding to the nearest whole number. I was wondering if there was a way to do this with math.round or if there is a different solution. Thanks!
Method 1: The quick way is to use toFixed() method like this:
var num = 2.12;
var round = num.toFixed(1); // will out put 2.1 of type String
One thing to note here is that it would round 2.12 to 2.1 and 2.15 to 2.2
Method 2: On the other hand you can use Math.round with this trick:
var num = 2.15;
Math.round(num * 10) / 10; // would out put 2.2
It would round to the upper bound.
So, choose whichever you like.
Also if you use a modern version of JS ie. ES then using const and let instead for variable declaration might be a better approach.
NOTE: remember that .toFixed() returns a string. If you want a number, use the Math.round() approach. Thanks for the reminder #pandubear
Math.round(X); // round X to an integer
Math.round(10*X)/10; // round X to tenths
Math.round(100*X)/100; // round X to hundredths
Math.round(1000*X)/1000; // round X to thousandths

How can I parse a string as an integer and keep decimal places if they are zeros?

I have these strings: "59.50" & "30.00"
What I need to do is convert them to integers but keep the trailing zeros at the end to effectively return:
59.50
30.00
I've tried:
Math.round(59.50 * 1000) / 1000
Math.round(30.00 * 1000) / 1000
but ended up with
59.5
30
I'm assuming I need to use a different method than Math.round as this automatically chops off trailing zeros.
I need to keep these as integers as they need to be multiplied with other integers and keep two decimals points. T thought this would be fairly straight forward but after a lot of searching I can't seem to find a solution to exactly what I need.
Thanks!
Your premise is flawed. If you parse a number, you are converting it to its numerical representation, which by definition doesn't have trailing zeros.
A further flaw is that you seem to think you can multiply two numbers together and keep the same number of decimal places as the original numbers. That barely makes sense.
It sounds like this might be an XY Problem, and what you really want to do is just have two decimal places in your result.
If so, you can use .toFixed() for this:
var num = parseFloat("59.50");
var num2 = parseFloat("12.33");
var num3 = num * num2
console.log(num3.toFixed(2)); // 733.64
Whenever you want to display the value of the variable, use Number.prototype.toFixed(). This function takes one argument: the number of decimal places to keep. It returns a string, so do it right before viewing the value to the user.
console.log((123.4567).toFixed(2)); // logs "123.46" (rounded)
To keep the decimals - multiply the string by 1
example : "33.01" * 1 // equals to 33.01
Seems you are trying to retain the same floating point, so better solution will be some thing like
parseFloat(string).toFixed(string.split('.')[1].length);
If you want numbers with decimal points, you are not talking about integers (which are whole numbers) but floating point numbers.
In Javascript all numbers are represented as floating point numbers.
You don't need the trailing zeros to do calculations. As long as you've got all the significant digits, you're fine.
If you want to output your result with a given number of decimal values, you can use the toFixed method to transform your number into a formatted string:
var num = 1.5
var output = num.toFixed(2) // '1.50'
// the number is rounded
num = 1.234
output = num.toFixed(2) // '1.23'
num = 1.567
output = num.toFixed(2) // '1.57'
Here's a more detailed description of toFixed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Javascript coding a calculation

I'm coding a price calculator in JS and I'm stuck with one formula:
number = (parseFloat(newnumber, 10) * parseFloat(1.536, 10)).toString(10);
I want to add 7.44 to the value of newnumber, before it is multiplied with 1.536
I've tried several things, but with no success.
Going to submit this as an answer, even though someone has put this up a comment while I was typing my answer.
number = ((+newnumber + 7.44) * 1.536).toString();
That should give you a string representation of the summed value.
Use parentheses to make the addition before the multiplication.
number = ((parseFloat(newnumber) + 7.44) * 1.536).toString();
Notes: parseFloat doesn't have a radix parameter. There is no reason to parse the number 1.536, that will only turn it to a string and then back to the same number again. The default for the radix parameter for toString is 10, so that isn't needed.
number = ((parseFloat(newnumber) + 7.44) * parseFloat(1.536)).toString();?
Just use parentheses to separate out the operations. Simple fix.
Working DEMO
Try the following code -
var newnumber = '1';
var number = ((parseFloat(newnumber) + 7.44) * parseFloat(1.536)).toString(10);
alert(number);

Why am I losing the decimal when working this equation in Javascript?

I am working on a calculator to figure out how much of a substance is still in someones system after x amount of days. The three variables are the amount taken daily, the number of days it has been taken, and the half-life of the substance. My formula below may not be 100% correct, but that's not the problem I'm having at this time.
I'm having problems with the answer being converted to an integer. Where am I going wrong? I need everything to stay with a float (decimal) value.
P.S. Ignore comments, they were just added on here to explain variables.
dose = 8.00 // 8mg dose
meta = 70.0 // 70 hour half-life
days = 5 // 5 days of use
sys = dose * (1.0/2.0) ^ (24.0/meta)
for(x=2; x <= days; x++){
sys = ((sys+dose) * (1.0/2.0) ^ (24.0/meta))
}
Thanks in advance!
^ in javascript isn't the mathematical 'raise to a power' operator, it is the bitwise XOR operator, and will always give a integer. You want to use pow from the Math class.
Try
sys = dose*Math.pow((1.0/2.0), (24.0/meta))
and
sys = (sys+dose)*Math.pow((1.0/2.0), (24.0/meta))
instead
Yeah, JavaScript automatically casts whole numbers to integers. Once it has a decimal value (other than 0), e.g. 1.5, it should respect the float.

Convert a Decimal number to float and truncate it

I have a number
For example:
8183
What I need is to convert it to a float number-
For example 8183
(8183).toFixed(2);
will return me
8183.00
But I need to truncate it further, so the final number will be
8.18
So basically I need to make it float number with just 2 decimal places.
I tried using the Math.floor and ceil but couldnt figure it out!
Well what you're trying to accomplish is not completely clear, but I think that if you start by dividing by 1000, then call toFixed on it, it will give you the desired result.
var before = 8183;
var after = (before / 1000).toFixed(2); //8.18
You could divide by 10 until you are less than 10:
var digits = 8183;
while((digits = digits/10) > 10) {}
digits = digits.toFixed(2); // 8.18
For negative numbers, you could want to store a boolean value and use Math.abs(digits).
For numbers less than 0, you would want to multiple instead of divide.
If all you really want is scientific notation use toExponential(2)

Categories

Resources