This question already has answers here:
Is there a way to round numbers into a reader friendly format? (e.g. $1.1k) [closed]
(2 answers)
1000000 to 1M and 1000 to 1K and so on in JS [duplicate]
(1 answer)
Closed 5 years ago.
I want to trim my 5 or more than 5 digit number 12345 to
12.34k
to fit it in my small box. Is there any util to use?
you can divide it by 1000 and then specify the number of digits you want using toFixed()
let x = 12345;
x = x/1000;
x = x.toFixed(2);
x = x + "k";
console.log(x)
Related
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
JavaScript displaying a float to 2 decimal places
(14 answers)
Closed 1 year ago.
i am working with crypto currency wallets and every coin have its own decimal values
like btc has upto 8 decimal
eth has upto 18 decimal
i am facing the issue with decimal fixing
var amount = "0.224424";
var fee = "0.006069";
var t_amount = amount - fee;
t_amount = Number((t_amount).toFixed(18));
and the anwer i am getting in t_amount variable is
0.21835500000000002
but i dont want this value like it has many zeros and at the end a 2
i want this like below
0.21835500000000002 => 0.218355
0.018565000005 => 0.018565
0.0013320001 => 0.001332
anyone have the idea how to fix this issue..?
Just change your toFixed(18) into toFixed(6):
var amount = "0.224424";
var fee = "0.006069";
var t_amount = amount - fee;
t_amount = Number((t_amount).toFixed(6));
console.log(t_amount)
This question already has answers here:
How to round an integer up or down to the nearest 10 using Javascript
(4 answers)
Closed 6 years ago.
I need to round a number to the nearest multiple of 27 with JavaScript. It would be better if I could always round UP to the number, but it would also be useful to know how to round to the closest multiple of 27 (whether up or down). It doesn't have to be vanilla JavaScript it could be jQuery too.
Divide the number by 27, round (or .ceil to round it up) the result and multiply by 27:
var x = 28;
console.log('round', Math.round(x/27) * 27);
console.log('ceil', Math.ceil(x/27) * 27);
var y = 47;
console.log('round', Math.ceil(y/27) * 27);
console.log('ceil', Math.round(y/27) * 27);
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I have a JavaScript function that returns the result of multiplications using decimal values (four digits after dot).
But, in some conditions, the result is a mess like this:
3.9050 * 9 = 35.144999999999996.
What should I do to normalize those results?
Use .toFixed(numberOfDecsYouWant)
var num = 3.9050 * 9;
console.log(num); //35.144999999999996
console.log(num.toFixed(4)) //35.1450
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript - ceiling of a dollar amount
I have a number like this: 360.654444444447
I want this to round up to 360.66
How do i do it? The amount of 4's between the 5 and 7 is unknown.
EDIT: The key issue here is that when the decimals after the 2nd would round to 5, it should round up. (ie: 360.654447 can be rounded to 360.655 - and that should round to 360.66
It's similar to the PHP_HALF_ROUND_MODE thing.
Use the ceil method, not the round method as others have suggested.
var number = 360.654444444447;
var result = Math.ceil(number * 100) / 100;