How to format numbers in JavaScript? - javascript

How to format numbers in JavaScript?
JavaScript culture sensitive currency formatting

The best you have with JavaScript is toFixed() and toPrecision() functions on your numbers.
var num = 10;
var result = num.toFixed(2); // result will equal 10.00
num = 930.9805;
result = num.toFixed(3); // result will equal 930.981
num = 500.2349;
result = num.toPrecision(4); // result will equal 500.2
num = 5000.2349;
result = num.toPrecision(4); // result will equal 5000
num = 555.55;
result = num.toPrecision(2); // result will equal 5.6e+2
Currency, commas, and other formats will have to be either done by you or a third party library.

The improved script (the previous was buggy, sorry; to be honest I haven't tested this exaustively either), it works like php number_format:
function formatFloat(num,casasDec,sepDecimal,sepMilhar) {
if (num < 0)
{
num = -num;
sinal = -1;
} else
sinal = 1;
var resposta = "";
var part = "";
if (num != Math.floor(num)) // decimal values present
{
part = Math.round((num-Math.floor(num))*Math.pow(10,casasDec)).toString(); // transforms decimal part into integer (rounded)
while (part.length < casasDec)
part = '0'+part;
if (casasDec > 0)
{
resposta = sepDecimal+part;
num = Math.floor(num);
} else
num = Math.round(num);
} // end of decimal part
while (num > 0) // integer part
{
part = (num - Math.floor(num/1000)*1000).toString(); // part = three less significant digits
num = Math.floor(num/1000);
if (num > 0)
while (part.length < 3) // 123.023.123 if sepMilhar = '.'
part = '0'+part; // 023
resposta = part+resposta;
if (num > 0)
resposta = sepMilhar+resposta;
}
if (sinal < 0)
resposta = '-'+resposta;
return resposta;
}

Related

Error in true false output in Array Problem

Here's the question:
A Narcissistic Number is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).
For example, take 153 (3 digits), which is narcisstic:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Your code must return true or false (not 'true' and 'false') depending upon whether the given number is a Narcissistic number in base 10.
My Code is:
function narcissistic(value) {
let vLen = value.length;
let sum = 0;
for (let i = 0; i < vLen; i++) {
sum += Math.pow(value[i], vLen);
}
if (sum == value) {
return true;
} else {
return false;
}
}
But I'm getting errors. What should I do?
Numbers don't have .length, convert to string first
vLen[i], you cant treat a number as array, again, convert to string to use that syntax.
The return can be simplefied to return (sum === value);
function narcissistic(value) {
let sVal = value.toString();
let vLen = sVal.length;
let sum = 0;
for (let i = 0; i < vLen; i++) {
sum += Math.pow(sVal[i], vLen);
}
return (sum === value);
}
console.log(narcissistic(153));
console.log(narcissistic(111));
Well... There are several things wrong with this code, but I think there is mostly a problem with the types of your input.
I'll show you how you can cast the types of your input to make sure you work with the types you need:
Also... You should try to avoid using the == operator and try to use === instead (same goes for != and !==), because the == and != don't try to match the types, resulting in sometimes unpredictable results
function narcissistic(value) {
valueStr = String(value);
let vLen = valueStr.length;
let sum = 0;
for (let i = 0; i < vLen; i++) {
sum += Number(valueStr[i]) ** vLen;
}
if (sum === value) {
return true;
} else {
return false;
}
}
if(narcissistic(153)) {
console.log("narcissistic(153) is true!") // expected value: true
}
All the first 9 digits from 1 to 9 is Narcissistic number as there length is 1 and there addition is always same.
So, first we are checking weather the number is greater than 9 or not.
if(num>9) =>false than it's a narcissistic number.
-if(num>9) =>true than we have to split number into digits for that I have used x = num.toString().split('');. Which is first converting number to String and than using split() function to split it.
Than , we are looping through each digit and digitsSum += Math.pow(Number(digit), x.length); adding the power of digit to const isNarcissistic = (num) => { let x = 0; let digitsSum.
at the end, we are comparing both num & digitsSum if there are matched than number is narcissistic else not.
const isNarcissistic = (num) => {
let x = 0;
let digitsSum = 0;
if (num > 9) {
x = num.toString().split('');
x.forEach(digit => {
digitsSum += Math.pow(Number(digit), x.length);
});
if (digitsSum == num) {
return true;
} else {
return false;
}
} else {
return true;
}
}
console.log(isNarcissistic(153));
console.log(isNarcissistic(1634));
console.log(isNarcissistic(1433));
console.log(isNarcissistic(342));

JavaScript: Hex to decimal converter error

I have been making a custom function for converting hex to decimal from my scratch project:
function Hex2Decimal(hex){
var deci = 0;
var num = 1;
var hexstr = String(hex);
hexstr = hexstr.toLowerCase();
var expon = 0;
for(var i = 0; i < hex.length; i++){
expon = Math.pow(16,hexstr.length - (num+1));
if(hexstr[num+1] === "a"){
deci = (10*expon)+deci;
}else if(hexstr[num-1] === "b"){
deci = (11*expon)+deci;
}else if(hexstr[num-1] === "c"){
deci = (12*expon)+deci;
}else if(hexstr[num-1] === "d"){
deci = (13*expon)+deci;
}else if(hexstr[num-1] === "e"){
deci = (14*expon)+deci;
}else if(hexstr[num-1] === "f"){
deci = (15*expon)+deci;
}else if(hexstr[num-1] != "undefined"){
deci = (Number(hexstr[num-1])*expon)+deci;
}
num = num + 1;
}
return deci;
}
but when I put "BC324240" into it, it returns the value '197338148' instead of '3157410368.'
When converting the value back to hex, I get 'BC32424.' For some reason, that I need help finding, the '0' in it is completely 'ignored.'
Also noticed that using '10' returns 1...
The following built-in function will do the conversion for you:
dec = parseInt('0x' + hexstr,16);
Just be sure that the number to convert is less than the maximum safe JavaScript integer:
(2^53 - 1) = 0x1fffffffffffff = 9007199254740991.
If you need to work with larger numbers, look at the code here:
https://codegolf.stackexchange.com/questions/1620/arbitrary-base-conversion
I didn't write it, so don't ask me to explain it
You're missing the last position (the digit you should multiply by 16^0) because your call:
expon = Math.pow(16, hexstr.length - (num+1));
is off by one, should be:
expon = Math.pow(16, hexstr.length - num);

JavaScript: formatting input method gives wrong values

I'm programming a method in JavaScript/JQuery which converts the value an user enters in an inputbox. The meaning is to make this input regional aware.
The functionality contains removing zeros at the beginning, placing thousand seperators and a decimal separator.
In this use case is the , symbol a thousand separator and the . dot the decimal separator
For example following input gets converted in following output.
12300 => 12,300.00
100 => 100.00
1023.456 => 1,023.456
Now There is still a problem with numbers, less than 100.
For example following input is malformed:
1 => 1,.00
2.05 => .05
20 => 20,.00
25.65 => .65
When I don't enter a decimal value in the input box, I get an unneeded thousand separator. When I enter a decimal value, I lose my content before the decimal separator.
The code:
$("#queryInstructedAmountFrom").change(function(){
var amount = $("#queryInstructedAmountFrom").val();
amount = removeZeros(amount);
var nonFractions = amount.match(/.{1,3}/g);
if(nonFractions == null) {
nonFractions = [];
nonFractions.push(amount);
}
var splittedValues = amount.split(/[,.]/);
amount = "";
if(splittedValues.length == 1) {
amount += splittedValues[0];
nonFractions = amount.match(/.{1,3}/g);
var firstIndex = amount.length % 3;
if(firstIndex != 0) {
var firstNumbers = amount.substr(0, firstIndex);
amount = amount.substr(firstIndex);
nonFractions = amount.match(/.{1,3}/g);
if(nonFractions == null) {
nonFractions = [];
nonFractions.push(amount);
}
amount = "";
amount += firstNumbers;
amount += thousandSeparator;
} else {
amount = "";
}
for(var i=0 ; i < nonFractions.length ; i++) {
amount += nonFractions[i];
if(i < (nonFractions.length - 1) && nonFractions.length != 1){
amount += thousandSeparator;
}
}
amount += decimalSeparator;
amount += "00";
} else {
for(var i=0 ; i < splittedValues.length - 1 ; i++) {
amount += splittedValues[i];
}
nonFractions = amount.match(/.{1,3}/g);
var firstIndex = amount.length % 3;
if(firstIndex == 0) {
nonFractions = amount.match(/.{1,3}/g);
}
if(firstIndex >= 1 && nonFractions != null) {
var firstNumbers = amount.substr(0, firstIndex);
amount = amount.substr(firstIndex);
nonFractions = amount.match(/.{1,3}/g);
if(nonFractions != null) {
amount = "";
amount += firstNumbers;
amount += thousandSeparator;
} else {
nonFractions = [];
nonFractions.push(amount);
}
} else {
amount = "";
}
for(var i=0 ; i < nonFractions.length ; i++) {
amount += nonFractions[i];
if(i < (nonFractions.length - 1) && nonFractions.length != 1){
amount += thousandSeparator;
}
}
amount += decimalSeparator;
amount += splittedValues[splittedValues.length -1];
}
$("#queryInstructedAmountFrom").val(amount);
});
});
function removeZeros(amount) {
while (amount.charAt(0) === '0') {
amount = amount.substr(1);
}
if(amount.length == 0){
amount = "0";
}
return amount;
}
What is going wrong?
What is going wrong?
I'd say almost everything. You have very unclear, messy code, I hardly following your logic, but you have several critical logic mistakes in code, for example:
1
1 is converted to 1,.00 because:
var splittedValues = amount.split(/[,.]/);
creates array with single element ['1']
var firstIndex = amount.length % 3;
1%3 == 1, so you're going into if condition, where amount += thousandSeparator; appends thousand separator, but you should add separator only if you have something after that
2
2.05 is wrong, because it goes into this branch:
var firstNumbers = amount.substr(0, firstIndex); // stores '2' into firstNumbers
amount = amount.substr(firstIndex); // sets amount to empty string
later, nonFractions is null:
nonFractions = [];
nonFractions.push(amount);
but firstNumbers is not used at all, ie its value is lost
3
also, you have:
nonFractions = amount.match(/.{1,3}/g);
var firstIndex = amount.length % 3;
if (firstIndex == 0) {
nonFractions = amount.match(/.{1,3}/g);
}
what is the sense of nonFractions re-init?
probably there are more errors and edge cases where this code fails, I suggest you to use library (like in other answers) or if you want to have your own code, here is simple version you can use:
$(document).ready(function() {
$("#queryInstructedAmountFrom").change(function() {
var val = parseFloat(('0' + $("#queryInstructedAmountFrom").val()).replace(/,/g, '')); // convert original text value into float
val = ('' + (Math.round(val * 100.0) / 100.0)).split('.', 2);
if (val.length < 2) val[1] = '00'; // handle fractional part
else while (val[1].length < 2) val[1] += '0';
var t = 0;
while ((val[0].length - t) > 3) { // append thousand separators
val[0] = val[0].substr(0, val[0].length - t - 3) + ',' + val[0].substr(val[0].length - t - 3);
t += 4;
}
$("#queryInstructedAmountFrom").val(val[0] + '.' + val[1]);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="queryInstructedAmountFrom">
Why don't you use jQuery-Mask-Plugin?
<input type="text" id="money" />
and just invoke the plugin:
$('#money').mask('000.000.000.000.000,00', {reverse: true});
Plunker: https://plnkr.co/edit/PY7ihpS3Amtzeya9c6KN?p=preview
Refer to the below code updated.
$(document).ready(function() {
$("#queryInstructedAmountFrom").change(function() {
var amount = $("#queryInstructedAmountFrom").val();
amount = removeZeros(amount);
// format amount using 'ThousandFormattedValue' function
amount = ThousandFormattedValue(amount);
$("#queryInstructedAmountFrom").val(amount);
});
});
function removeZeros(amount) {
while (amount.charAt(0) === '0') {
amount = amount.substr(1);
}
if (amount.length == 0) {
amount = "0";
}
return amount;
}
function ThousandFormattedValue(iValue) {
// declaring variables and initializing the values
var numberArray, integerPart, reversedInteger, IntegerConstruction = "",
lengthOfInteger, iStart = 0;
// splitting number at decimal point by converting the number to string
numberArray = iValue.toString().split(".");
// get the integer part
integerPart = numberArray[0];
// get the length of the number
lengthOfInteger = integerPart.length;
// if no decimal part is present then add 00 after decimal point
if (numberArray[1] === undefined) {
numberArray.push("00");
}
/* split the integer part of number to individual digits and reverse the number
["4" , "3" , "2" , "1"] - after split
["1" , "2" , "3" , "4"] - after reverse
"1234" - after join
*/
reversedInteger = integerPart.split("").reverse().join("");
// loop through the string to add commas in between
while (iStart + 3 < lengthOfInteger) {
// get substring of very 3 digits and add "," at the end
IntegerConstruction += (reversedInteger.substr(iStart, 3) + ",");
// increase counter for next 3 digits
iStart += 3;
}
// after adding the commas add the remaining digits
IntegerConstruction += reversedInteger.substr(iStart, 3);
/* now split the constructed string and reverse the array followed by joining to get the formatted number
["1" , "2" , "3" , "," ,"4"] - after split
["4" , "," , "3" , "2" , "1"] - after reverse
"4,321" - after join
*/
numberArray[0] = IntegerConstruction.split("").reverse().join("");
// return the string as Integer part concatinated with decimal part
return numberArray.join(".");
}

generate n length unique random numbers using javascript

Following are the rules:
Generate random numbers of length N.
Not more than two consecutive digits in a number should be the same.
I written code, but even though 2 same consecutive digits in a number are allowed, it's not working. All unique random numbers are generated.
I keep one temporary variable, which stores the previous number generated, if it matches the present generate number, then discard and generate again.
while (rnum.length<=7)
{
gennum = randomNumericChar();
if(rnum.length==0) {
rnum = rnum.concat(gennum);
prevnum = gennum;
} else if(gennum!=prevnum) {
rnum = rnum.concat(gennum);
prevnum = gennum;
}
}
return rnum;
FOund solution:
Hi, i got the solution. ` var rnum = "" ;
var random;
while(rnum.length<=7)
{
random= generaterandom();
if(rnum.length<2)
{
rnum = rnum.concat(random);
}else
{
//check whether previous two numbers are same, if not then append generated random number
if(!(random==rnum.charAt(rnum.length-1) && random==rnum.charAt(rnum.length-2)))
{
rnum = rnum.concat(random);
}
}
}`
This might be your solution:
function randomNumericChar(lastNumber) {
var num = Math.floor((Math.random() * 10) + 1);
if (lastNumber !== null && num === lastNumber) {
return randomNumericChar(lastNumber);
} else {
return num;
}
}
function createNumber(n) {
var randomNumber = '';
var lastChar = null;
while (randomNumber.length < n) {
var num = randomNumericChar(lastChar);
if (((randomNumber.length + 1) % 2) === 1) {
lastChar = num;
}
randomNumber += num;
}
return randomNumber;
}
var randomNumber = createNumber(10);
Edit: forgot about the fact that there might be 1 consecutive number.

How to use Javascript math on a version number

I use jQuery to get the browser version like this:
var x = $.browser.version;
I get a string like this: 1.9.1.1
Now, I want to do an evaluation so if x is >= 1.9.1 then do some stuff. Unfortunately, with multiple decimal points, I cannot do a parseFloat() because it converts 1.9.1.1 to simply 1.9, and the if evaluation would match a 1.9.0 version (which I do not want).
Has someone figured out a way to accomplish turning a version number (with multiple decimals) into something that can be used as a number for evaluation (or some other way to accomplish what I am trying to do here)?
Thanks -
You could do something with string.split and then do a digit by digit comparison
// arr[0] = 1
// arr[1] = 9
// arr[2] = 1
// arr[3] = 1
var arr = ($.browser.version).split('.');
The following is taken from this post
This is a function that will parse your version string and give you back a JSON object
function parseVersionString (str) {
if (typeof(str) != 'string') { return false; }
var x = str.split('.');
// parse from string or default to 0 if can't parse
var maj = parseInt(x[0]) || 0;
var min = parseInt(x[1]) || 0;
var bld = parseInt(x[2]) || 0;
var rev = parseInt(x[3]) || 0;
return {
major: maj,
minor: min,
build: bld,
revision: rev
}
}
Then you could use the following syntax
var version = parseVersionString($.browser.version);
// version.major == 1
// version.minor == 9
// version.build == 1
// version.revision == 1
Here's another version of versionCmp():
function versionCmp(v1, v2) {
v1 = String(v1).split('.');
v2 = String(v2).split('.');
var diff = 0;
while((v1.length || v2.length) && !diff)
diff = (+v1.shift() || 0) - (+v2.shift() || 0);
return (diff > 0) - (diff < 0);
}
Another possibility would be to assign a numeric value to each version number:
function valueOfVersion(ver) {
ver = String(ver).split('.');
var value = 0;
for(var i = ver.length; i--;)
value += ver[i] / Math.pow(2, i * 8) || 0;
return value;
}
This only works if each digit is less than 256 (because of the hard-coded divisor) and has a limited precision (ie the version strings can't get arbitrarily long).
You need to treat each portion of the string as a seperate integer, so split and iterate, and cmp:
// perform cmp(a, b)
// -1 = a is smaller
// 0 = equal
// 1 = a is bigger
function versionCmp(a, b) {
a = a.split(".");
b = b.split(".");
for(var i=0; i < a.length; i++) {
av = parseInt(a[i]);
bv = parseInt(b[i]);
if (av < bv) {
return -1;
} else if (av > bv) {
return 1;
}
}
return 0;
}
console.log(versionCmp("1.1.2.3", "1.2.1.0")); // should be -1
console.log(versionCmp("1.19.0.1", "1.2.0.4")); // should be 1
console.log(versionCmp("1.2.3.4", "1.2.3.4")); // should be 0
You could remove all dots and then parse it as an integer.
Take note tho, this solution doesn't work in the long term.

Categories