How do you make a price in javascript? ex $1.99 [duplicate] - javascript

This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 9 years ago.
I would like the output for each function to yield $x.xx as if it were a price. Being new to javascript is there a simple way to accomplish this or is it a complex system to get the results that I desire for my project?
Below is the code i have made in javascript:
//First function outputs a random number
var myFirstFunction = function(First)
{
return First;
};
var FirstAnswer = myFirstFunction(Math.random());
console.log(FirstAnswer);
//Second function outputs the random number multiplied by 10
var mySecondFunction = function(Second)
{
return Second * 10;
};
var SecondAnswer = mySecondFunction(FirstAnswer);
console.log(SecondAnswer);
//Third function divided the random number by 3.3
var myThirdFunction = function(Third)
{
return Third / 3.3;
}
var ThirdAnswer = myThirdFunction(SecondAnswer);
console.log(ThirdAnswer)

Use toFixed:
function price (number) {
return '$' + number.toFixed(2);
}
Here's the fiddle: http://jsfiddle.net/Uc7x3/

Related

JavaScript do wrong multiplication when the numbers are too larger || return round off of multiplication result [duplicate]

This question already has answers here:
Extremely large numbers in javascript
(5 answers)
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 3 months ago.
I am try to solve a problem of LEETCODE using JavaScript, the problem is Multiply Strings and the solution is:
const string = (num) => {
return '' + num
}
var multiply = function(num1, num2) {
let result = string(num1*num2)
return result
};
console.log(multiply("123456789",
"987654321"))
it should return "121932631112635269"
but it returns 121932631112635260
Any Solution?
"123456789" * "987654321" should return "121932631112635269"
but it returns 121932631112635260

I'm trying to create a function that follows the Luhn's Algorithm [duplicate]

This question already has answers here:
Implementation of Luhn algorithm
(14 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I tried to multiply each number whose index number is an even number by two, and that worked fine. Still, the problem lies here: If any of the results is greater than or equal to 10, then add up the two numbers, for example, if one of the results is 12, then add up 1 and 2, which should be equal to 3. So this is what I tried:
var num = 122345643345673;
var convNum = num.toString();
var aftertoString = convNum.split("");
for(let i = 1; i < aftertoString.length; i++){
if (i % 2 == 0) {
var multi = aftertoString[i] * 2;
if(multi > 10){
var multiStringed = multi.toString();
var aftermutliStringed = multiStringed.split("");
var first = parseInt(aftermutliStringed[2])
var multi = first + first;
}
console.log(multi);
}
}
Since any index of the "aftermultiSringed" array is not a number, I tried to convert it to a number using the "parseInt()" method, but the result is NaN, please why am I getting this.
The method parseInt usage is incorrect.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
// var first = aftermultiStringed[1].parseInt();
var first = parseInt(aftermultiStringed[1]);

How do I get 100000 to be like 100,000 [duplicate]

This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 4 years ago.
const renderCommas = (number) => {
return number >= 1000 ? `${Number.parseFloat((number).toFixed(3))}` : number;
};
you can use toLocaleString()
a = 100000
console.log(a.toLocaleString());
You can use the following code to format a number with a comma:
var num = 100000;
var formatted = num.toLocaleString(undefined);
console.log(formatted);

Javascript to turn 110,000 into 110K and not 0.11M [duplicate]

This question already has an answer here:
Format a javascript number with a Metric Prefix like 1.5K, 1M, 1G, etc [duplicate]
(1 answer)
Closed 7 years ago.
To start with you need...
function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
x-=x%3
return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}
Then calling like so...
// m( ANY NUMBER HERE or VAR LIKE I USE,HOW DECIMAL PLACES)
m(110000,2)
However instead of the above's result of 0.11M, I would like it to display 110k.
What you have there is an example of an overly optimized script, lets make it more developer friendly an readable
function metricPrefix(rawNumber,decimalPlaces){
var sufixes= " kMFGPE";
var numberLength =(''+n).length;
decimalPlaces=Math.pow(10,d); //raise 10 to the number of decimal places
var modLen = numberLength - numberLength%3;
var sufix = sufixes[modLen/3];
return Math.round(rawNumber*decimalPlaces/decimalPlaces(10,modLen))/decimalPlaces+ sufix;
}
Now it's easier to work with. We can see the issue is that we need to adjust for when the string is divisible by 3, so lets fix that.
function metricPrefix(rawNumber,decimalPlaces){
var sufixes= " kMFGPE";
var numberLength =(''+rawNumber).length;
decimalPlaces=Math.pow(10,decimalPlaces); //raise 10 to the number of decimal places
//THis is the change
//If the length is divisable by 3 take 3 off the length
var modLen = numberLength%3 == 0 ? numberLength - 3 - (numberLength%3) : numberLength - (numberLength%3);
console.log(modLen);
var sufix = sufixes[(modLen/3)]
console.log(sufix)
return Math.round(rawNumber*decimalPlaces/Math.pow(10,modLen))/decimalPlaces+ sufix;
}
$(document).ready(function(){
$("#result").html(metricPrefix(110000,2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>

JavaScript check if number is whole [duplicate]

This question already has answers here:
How do I check that a number is float or integer?
(52 answers)
Closed 7 years ago.
im trying to check if a number is a whole after a calculation. What I have so far prints out how many times one number gets divided by another, but when the number is not whole it dose not print anything out. Heres my code:
function round() {
var percent = document.getElementById('percent_sale').value;
var perShare = document.getElementById('singleShare').value;
var result = (percent / perShare);
if(result % 1 == 0) {
document.getElementById('results1').innerHTML = ('Number of shares:'+result);
} else {
document.getElementById(results1).innerHTML = ('number of shares must ');
}
}
The values get input buy a user, and the percent for sale is say 50 and the single share is say 2.5 this would return 20 shares.
What I need is if I put in something like 50 for sale and 3.15 single share it tells the user to make equal number of shares as it would return 15.87
Any ideas where ive gone wrong?
Convert your number into string and then check if the string contains only numbers
var num = 15;
var n = num.toString();
This will convert it into string then this
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
For further reference.Link StackOverFlow

Categories