JS use parseFloat,there are a lot of 0 in the decimal - javascript

I use this way to divide a decimal.I want to divide this number by 100.
var myNumber="1245.6699";
myNumber=""+parseFloat(myNumber)*10000/1000000;//"12.4566990000000002"
myNumber=Number(myNumber);//12.4566990000000002
I want this division to keep 6 digit decimal like this 12.456699,but now the result is 12.4566990000000002,how to modify?

Use toFixed() method of Number object:
var myNumber = "1245.6699";
console.log(
(myNumber / 100).toFixed(6)
)

Related

Rounding up to two decimal place in javascript eg 4.9995 to 4.99 in mathematical way in js

How can I convert decimal number to 2 decimal place number?
Example: I want to connvert 4.995 to 4.99 but javascript is returning 5.00.
var price=4.995;
var rounded_price=price.toFixed(2);
console.log(rounded_price);
I wouldn't call it rounding but you can achieve it by:
function trim2Dec(n) {
return Math.floor(n * 100) / 100;
}
alert(trim2Dec(4.995));
You can use regex for this as the following:
alert("4.995".replace(/(\d+(\.\d{1,2})?)\d*/, "$1"))
This is Pretty simple check out this code
var price=4.995;
var price1=4.985;
var rounded_price=(Math.round(price*100)/100);
var rounded_price1=(Math.round(price1*100)/100);
console.log("price : "+rounded_price+" price1 : "+rounded_price1);
here at first i am multiplying the price and then i have divided it with 100..just as we do to find the percentage of any number.

Javascript split integer and add decimal point

My integer value is 1210 and i want split this integer like 1 | 210 .Have to add decimal point on middle.
Eg:
var integer=1210;
Split this integer and add decimal value like this 1.210
Why don't you just divide the number by 1000
var x = 1210;
var y = 1210/1000; //1.210 number
var z = y+""; // 1.120 will be string here
console.log(y); // Will output 1.210
If you're always dealing with 4 digit numbers, dividing by 1000 will work (as mentioned in another answer) but you'll need to use toFixed to make sure javascript doesn't remove trailing zeros:
var x = 1210;
(x / 1000).toFixed(3) // => "1.210"
(x / 1000) + "" // => "1.21"
More generically you could use:
x=prompt('enter an integer');
xl=x.toString().length-1
alert((x/Math.pow(10,xl)).toFixed(xl));
(just make sure you enter an integer, preferably +ve, at the prompt)

JavaScript add decimal without using toFixed() method

I've got some values that are appended with 00's for cents by PHP. I need to add a decimal point to them.
val = 10000 (needs to turn into 100.00);
val.toFixed(2) = 10000.00 (no bueno);
val.magic() = 100.00 (perf!)
Thanks!
(val/100).toFixed(2) = 100.00;
If you have a string value in cents, a simple regular expression can be used to insert a decimal point:
function addPoint(s) {
return s.replace(/(\d\d)$/,'.$1');
}
var s = '1000';
alert(addPoint(s)); // 10.00

How to use division in JavaScript

I want to divide a number in JavaScript and it would return a decimal value.
For example: 737/1070 - I want JavaScript to return 0.68; however it keeps rounding it off and return it as 0.
How do I set it to return me either two decimals place or the full results?
Make one of those numbers a float.
737/parseFloat(1070)
or a bit faster:
737*1.0/1070
convert to 2 decimal places
Math.round(737 * 100.0 / 1070) / 100
(737/1070).toFixed(2); rounds the result to 2 decimals and returns it as a string. In this case the rounded result is 0.69 by the way, not 0.68. If you need a real float rounded to 2 decimals from your division, use parseFloat((737/1070).toFixed(2))
See also
Also you can to use [.toPrecision(n)], where n is (total) the number of digits. So (23.467543).toPrecision(4) => 23.47 or (1241876.2341).toPrecision(8) => 1241876.2.
See MDN
Try this
let ans = 737/1070;
console.log(ans.toFixed(2));
toFixed() function will do
to get it to 2 decimal places you can: alert( Math.round( 737 / 1070 * 100) / 100 )
with lodash:
const _ = require("lodash");
Use of _.divide() method
let gfg = _.divide(12, 5);
Printing the output
console.log(gfg)
2.4
credit

JavaScript displaying a float to 2 decimal places

I wanted to display a number to 2 decimal places.
I thought I could use toPrecision(2) in JavaScript .
However, if the number is 0.05, I get 0.0500. I'd rather it stay the same.
See it on JSbin.
What is the best way to do this?
I can think of coding a few solutions, but I'd imagine (I hope) something like this is built in?
float_num.toFixed(2);
Note:toFixed() will round or pad with zeros if necessary to meet the specified length.
You could do it with the toFixed function, but it's buggy in IE. If you want a reliable solution, look at my answer here.
number.parseFloat(2) works but it returns a string.
If you'd like to preserve it as a number type you can use:
Math.round(number * 100) / 100
Don't know how I got to this question, but even if it's many years since this has been asked, I would like to add a quick and simple method I follow and it has never let me down:
var num = response_from_a_function_or_something();
var fixedNum = parseFloat(num).toFixed( 2 );
with toFixed you can set length of decimal points like this:
let number = 6.1234
number.toFixed(2) // '6.12'
but toFixed returns a string and also if number doesn't have decimal point at all it will add redundant zeros.
let number = 6
number.toFixed(2) // '6.00'
to avoid this you have to convert the result to a number. you can do this with these two methods:
let number1 = 6
let number2 = 6.1234
// method 1
parseFloat(number1.toFixed(2)) // 6
parseFloat(number2.toFixed(2)) // 6.12
// method 2
+number1.toFixed(2) // 6
+number2.toFixed(2) // 6.12
Try toFixed instead of toPrecision.
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
round(1.005, 2); // return 1.01
round(1.004, 2); // return 1 instead of 1.00
The answer is following this link: http://www.jacklmoore.com/notes/rounding-in-javascript/
I used this way if you need 2 digits and not string type.
const exFloat = 3.14159265359;
console.log(parseFloat(exFloat.toFixed(2)));
You could try mixing Number() and toFixed().
Have your target number converted to a nice string with X digits then convert the formated string to a number.
Number( (myVar).toFixed(2) )
See example below:
var myNumber = 5.01;
var multiplier = 5;
$('#actionButton').on('click', function() {
$('#message').text( myNumber * multiplier );
});
$('#actionButton2').on('click', function() {
$('#message').text( Number( (myNumber * multiplier).toFixed(2) ) );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<button id="actionButton">Weird numbers</button>
<button id="actionButton2">Nice numbers</button>
<div id="message"></div>
The toFixed() method formats a number using fixed-point notation.
and here is the syntax
numObj.toFixed([digits])
digits argument is optional and by default is 0. And the return type is string not number. But you can convert it to number using
numObj.toFixed([digits]) * 1
It also can throws exceptions like TypeError, RangeError
Here is the full detail and compatibility in the browser.
let a = 0.0500
a.toFixed(2);
//output
0.05
There's also the Intl API to format decimals according to your locale value. This is important specially if the decimal separator isn't a dot "." but a comma "," instead, like it is the case in Germany.
Intl.NumberFormat('de-DE').formatToParts(0.05).reduce((acc, {value}) => acc += value, '');
Note that this will round to a maximum of 3 decimal places, just like the round() function suggested above in the default case. If you want to customize that behavior to specify the number of decimal places, there're options for minimum and maximum fraction digits:
Intl.NumberFormat('de-DE', {minimumFractionDigits: 3}).formatToParts(0.05)
float_num = parseFloat(float_num.toFixed(2))
I have made this function. It works fine but returns string.
function show_float_val(val,upto = 2){
var val = parseFloat(val);
return val.toFixed(upto);
}

Categories

Resources