Javascript: How cut out part of an integer? - javascript

How to cut out part of an integer?
What can i do to get an integer 12 or 123 from a variable n
let n = 123456;

A simple way to do this would be to just cast the input number to a string, and then take a substring:
var n = 123456;
n = n + "";
var output = n.slice(0, 2);
console.log(output);
Another approach, if all you want is to get some number of leading digits, would be to divide by the correct multiple of ten, e.g.
var n = 123456;
var output = Math.floor(output / 10000);

The strategy I suggest is to convert the integer value to a string which you may manipulate with parseInt() as well as the string's substr() method, as follows:
const START = 0;
const first_two = 2;
const first_three = 3;
const base_ten = 10;
let n = 123456;
let res = parseInt( String( n ).substr( START,first_two ),base_ten );
console.log(res);
res = parseInt(String( n ).substr( START,first_three ),base_ten );
console.log(res);
The Number Object has a toString() method but apparently it is safer to convert the integer to a string by passing the integer value of n to the String Object per this discussion. You may then use the String Object's substr() method to access the first two or three digits appearing in the string. Next, you may take the resulting numeric string and pass it to parseInt(), along with a base parameter to obtain the integer value.

I think this will work for you - you first change it to string and split it to an array and then remove the unwanted characters, then join the array together, and parse it as an integer: (edited to reflect #VLAZ's comment.
parseInt(n.toString().slice(0,2)));

Related

How to print out the even numbers in reverse order?

var num = prompt("Enter an integer : ");
var reversenum = num.reverse('');
document.write(reversenum);
I want to print out the even number of integers in reverse order after inputting an integer through the prompt, but I have no idea.
Even if I try to write split(), I don't think I can separate it because the letters are attached to each other. What should I do?
The result I want is,
Enter an integer : 8541236954
46248
Based on your updated question, I suppose what you want is to extract even-valued digits from a given integer, and display them in reverse order.
Since prompt() always returns a String, you can do one of the two ways to split it into digits and reverse their order:
Old-school JS way: num.split('').reverse()
ES6 array spread way: [...num].reverse()
Then, it is just a matter of using Array.prototype.filter() to return even numbers. Even numbers can be selected based on the criteria that their modulus of 2 is always 0 (i.e. when number is divided by 2, it has a remainder of 0).
Finally, join your filtered array so you get a string again.
See proof-of-concept order below:
const num = prompt("Enter an integer : "); // e.g. try '8541236954'
const digits = [...num].reverse();
const evenDigits = digits.filter(d => d % 2 ===0);
console.log(evenDigits.join('')); // e.g. '46248'
You need to first split the string into an array of characters, reverse the array and then join it again. I have given an easy to understand code which converts every character of the reversed string to an int and checks if that integer is even, followed by concatenating it in the answer.
var num = prompt("Enter an integer : ");
var reversenum = num.split('').reverse().join('');
var ans = "";
for (var i = 0; i < reversenum.length; i++)
{
var x = parseInt(reversenum[i]);
if(x%2 === 0)
ans = ans.concat(reversenum[i]);
}
console.log(ans);

Convert Large String Number to Number

I have this string
let a = '1589480087711852280'
How do I convert this to a number?
If I do
let a = '1589480087711852280'
a = Number(a)
console.log(a)
It prints 1589480087711852300 and just rounds it.
How do I get it to print 1589480087711852280?
It tried BigInt and that just puts an n at the end of the number.
I'm using this to query within mongodb so its not pulling the right search.
Use BigInt:
let a = '1589480087711852280'
a = BigInt(a)
console.log(a + 1n) // 1589480087711852281n

How can I get a count of the total number of digits in a number in TypeScript?

Declaration of a number variable called temp in TypeScript:
temp: number = 0.123;
How can I get a count of the total number of digits in a number (which is 3)?
If I understood correct, you want the part after ..
You can parse it into a string, split by . and count the string length of the second item.
const temp = 0.123;
const tempArray = temp.toString().split('.');
const length = tempArray[1].length;
console.log(length);
in typescript simply use:
this.number.toString().split('.'))[1].length
Where this.number is Type of Number
Use this Regexp \.\d+
var temp = 0.123;
var regex = new RegExp('\\.\\d+', 'g');
console.log(temp.toString().match(regex).pop().length - 1);

Trim long number in JavaScript

For example, I got that long nubmer 1517778188788. How can i get first 6 digits from that number, like 151777 and trim another digits?
Just convert a number to string and then slice it and convert it back to Number.
const a = 1517778188788;
const str_a = a.toString();
const result = Number(str_a.slice(0, 6));
new String(your_number).substring(0,6)
(basically converting it to a string and substringing it). Don't forget to parse it back afterwards
Applicable only when you want to strip last 7 digits, and the numbers have constant length (13 in this case). Still leaving you with first 6 ones though.
const nr = 1517778188788;
const result = Math.floor(nr / 10000000)
Try this:
var num = 1517778188788; // long number
var str = num.toString(); //convert number to string
var result = str.substring(0,6) // cut six first character
result = parseInt(result); // convert it to a number
here is a working fiddle

How to get ASCII of number in JavaScript?

I am aware of name.charCodeAt(0). I am having issued with the following code, so I want a solution for below.
var number= 2;
var t = number.charCodeAt(0);
console.log(t);
The answer needs to be in ASCII. I am getting the log as 2 and not as ASCII value 50. What can be the issue?
ASCII is a way of representing String data. You need to first convert your Number to String;
Remember also that String can have arbitrary length so you'll need to iterate over every character.
var x = 2,
str_x = '' + x,
chrs = Array.prototype.map.call(str_x, function (e) {return e.charCodeAt(0);});
chrs; // [50]
Finally, JavaScript works with UTF-16/UCS-2 and not plain ASCII. Luckily the values for digits are the same in both so you don't have to do any further transformation here.
You have to cast a number to a string first to use .charCodeAt to get the numerical character code.
var number = 2;
var t = String( number ).charCodeAt( 0 );
console.log( t ); // 50
Just convert the number to String and call the method on it.
var number = 2;
var numberInString = number.toString();
var code = numberInString.charCodeAt(0);
console.log(code);

Categories

Resources