Format money value into pounds and pence [duplicate] - javascript

This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 6 years ago.
I have helper function formatting my money value into pounds and pence:
formatMoney: function(10496.470000000001){
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
},
Gives me:
11,496.471,001,001,001
Is there a way I can format this accurately into pounds and pence? so it reads, £11,496.471p

In addition to the toFixed method call mentioned previously, the second parameter to replace should be "$1," rather than "1,". Also, your conversion to a number is somewhat fragile. Here's my attempt at remedying these issues:
function convert(value){
return "£"+((Number(value)||0).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"))+"p";
}
console.log(convert(10496.470000000001));

The Regexp doesn't care about the decimals you pass in. You need to convert your input to already have 2 decimals:
return value.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
^^^^^^^^^^
function convert(value){
return value.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
}
console.log(convert(10496.470000000001));

Related

JavaScript : How to format the amount decimal to 2 digits [duplicate]

This question already has answers here:
Format number to always show 2 decimal places
(37 answers)
Closed 11 months ago.
I tried to format the Amount field to 2 digits using the
Amount.toLocaleString()
toLocaleString(undefined, { minimumFractionDigits: 2 }),
Some of the Amount values are displayed correctly but few are incorrect
like $52.85.00 should be displayed as $52.85
function financial(x) {
return Number.parseFloat(x).toFixed(2);
}
console.log(financial(123.456));
console.log(financial(0.004));`
You can use Number.prototype.toFixed() to format with a number of decimals. Note: The toFixed method will return a string, so you should convert back to an floating number if your goal is not just to display. For more info on .toFixed() method access document
let numberToBeFormatted = 212.121212;
let formattedNumber = parseFloat(numberToBeFormatted.toFixed(2))

How can I make this js code output to one decimal place? How do implement in MY SPECIFIC case? [duplicate]

This question already has answers here:
How do you round to 1 decimal place in Javascript?
(24 answers)
Closed 4 years ago.
This code outputs a number with many decimal places.. I'd like it limited to just one... this code gets me my desired result BUT with too many characters displayed.
I'm a total noob....
function oninputchange() {
$output.textContent = $input.value.split(/\s/g).map(syllable).reduce(sum)/4.7;
}
The results are expected... Just want fewer decimal places
Use method toFixed() for limit the decimal number to show

In javascript parsing a string gives me huge number, why? [duplicate]

This question already has answers here:
How to convert a String containing Scientific Notation to correct Javascript number format
(8 answers)
Closed 5 years ago.
I have a string in response "2.222222522879E12",
When I do
parseFloat("2.222222522879E12")
2222222522879
But the expected value should be 2.22,
Whats happening ?
And how can I fix it
You're going to get 2.222222522879* 10^12 with that expression as E means exponent, or to the power of
As per your edit asking how to fix:
use the Number.prototype.toPrecision() function. Here is the documentation

How to round numbers with N digits after the zero? [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 6 years ago.
lets say I have a number in the following form: 0.00N1N2N3...(for example 0.007).
I want to round the number 0.00N1N2N3...Nn, into the follwoing number:
0.0M1M2M3..Mn.
For example:0.007 need to be round to 0.01.
Now the number can be also in the following form 0.N1...Nn or N1.N2...Nn so the solution need to be generic for all cases.
I have write the following function(Not sure if this is the right answer):
function roundup(number, precision) {
return Math.ceil(number * precision)
}
If the variable is float you can use toFixed() like
var formatted = parseFloat("345.65894").toFixed(2);
On most browsers you can use the toFixed() function.
number.toFixed(precision)
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

(Js) number method to add a comma after the first number and remove the 2 number after comma ?? is there any? [duplicate]

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);

Categories