How to cut out part of an integer?
What can i do to get an integer
123 from a variable
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);
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);
Math.trunc(), BigInt numbers, to represent integers of arbitrary length. They are These functions cover all of the possible ways to deal with the decimal part of a number. But what if Just round them to cut “tails” when needed. The funny� Optional. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array. If omitted, it acts like "0" end: Optional. An integer that specifies where to end the selection. If omitted, all elements from the start position and to the end of the array will be
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.
either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut. labels. labels for the levels of the resulting category. By default, labels are constructed using "(a,b]" interval notation.
- what is the condition here? need to cut off all the occurences of
- What is the logic of getting that part. Do you provide length to function
- Wait...it actually has a demo :P
- We still do not know what the OP's required condition is
- Thanks for your answer Tim!
- Thanks for your answer!
- You are correct, missed that one. Thank you - I already edited it.