I am beginner of JavaScript programming, when I check for parseInt() i.e., which converts string into an Integer Number, I am getting some different outputs.
var temp = 030;
document.writeln(" temp value after parseInt is =" + parseInt(temp));
output : temp value after parseInt is =24
I did n't find the reason why it is showing 24. Could you please help me in this. Thanks
The reason is because it is treated as octal format. Note that if the string begins with "0x", the radix is 16 (hexadecimal) and if the string begins with "0", the radix is 8 (octal).
To get the correct output just try like this:
alert(parseInt('030',10));
DEMO
because you have prefixed with 0 so it will be interpreted as octal number
so 030 will beconverted to decimal and that is 24
ParseInt is a method to convert a string to number.
You can add a second parameter in your parseInt to spedify the mathematical base.
For example:
parseInt('030',10)
If radix is undefined or 0 (or absent), JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
mb u mean?:
var temp = '030';
document.writeln(" temp value after parseInt is =" + parseInt(temp));
Related
In JavaScript, I want to remove the decimal place & the following zeros.
For example, my original number: "0.00558", I want to be left with "558".
Is this possible? (I'm also using AngularJS if that has a method for this).
you can do that by a simple regex replace.
var number = "0.00558";
number = number.replace(/^[0\.]+/, "");
console.log(number);//number is now "558"
Remove the decimal point:
el.value.replace(/[^\d]/g,'');
or
el.value.replace(/\./g,'');
Then get the integer value:
var el.value = parseInt(el.value);
While I have always gone with the default radix being 10, #aduch makes a good point. And MDN concurs.
The safer way is:
var el.value = parseInt(el.value,10);
From MDN:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
This question already has answers here:
How to output numbers with leading zeros in JavaScript? [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to convert a string to number, which is "09"
parseInt('09') //returns 9 always
What is the best way to get the number with leading zero and it would be better if someone explain, why the designed parseInt in such a way that doesn't care about leading zeros.
An int cannot have leading zeroes, since they don't have any mathematical meaning.
To store a number, languages use a binary representation (don't forget everything is 0s and 1s in the end). This representation is the same for 9, 09, or 00009. When the number must be printed, it is converted back to a string representation, so you lose the leading zeroes.
If you need to store/remember the 0s, your only choice is to store the string representation of the number.
What you could do is storing both the number and string representation, like this:
function MyInt(s){
this.asString = s;
this.num = parseInt(s);
}
var i = new MyInt("09");
console.log(i.num); // 9
console.log(i.asString); // 09
Take note that leading 0 don't have any value for int. If want to use for display purpose use string and for calculation part use parseInt(someval)
parseInt takes two parameters: string and radix.
radix tells parseInt how to convert a string to a number.
If you leave the radix out, the rules are somewhat simple for how it decides what radix to use:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
If the input string begins with "0", radix is eight (octal) or 10 (decimal).
Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
That said, you are using a function whose non-abbreviated name is "Parse Integer." You cannot make it retain leading zeros, because leading zeros have no mathematical value.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
Parsing a string using parseInt method returns invalid output .
Code :
parseInt("08");
Excepted Output :
8
Real Output :
0
Code [This returns output correctly] :
parseInt("8")
Output :
8
Why it happens ?
You need to specify the base:
parseInt("08",10); //=>8
Otherwise JavaScript doesn't know if you are in decimal, hexadecimal or binary.
(This is a best practise you should always use if you use parseInt.)
Also see Number:
Number("08"); // => 8
What is the difference between parseInt() and Number()?
What is the difference between parseInt(string) and Number(string) in JavaScript?
You should tell parseInt its 10 based:
parseInt("08", 10);
JavaScript parseInt() Function
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is
deprecated If the string begins with any other value, the radix is 10
(decimal)
http://jsfiddle.net/YChK5/
Strings with a leading zero are often interpreted as octal values. Since octal means, that only numbers from 0-7 have a meaning, "08" is converted to "0". Specify the base to fix this problem:
parseInt("08", 10); // base 10
As usual, the MDN is a good source of information:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt#Octal_Interpretations_with_No_Radix
Can anybody give me a little advice please?
I have a string, for example "01001011" and what I need to do is to reverse it, so I used .split('') than .reverse() and now I need to read the array as a string and convert it to integer. Is it possible?
Thanks
If you want to convert the array back to a string use join() (MDN) and for converting a string to an integer use parseInt() (MDN). The second argument of the later is an optional radix.
JavaScript will try to determine, what radix to use, but to be sure you should always add your radix manually. Citing from MDN:
If radix is undefined or 0, JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
So in your case the following code should work:
var a = '01001011';
var b = parseInt( a.split('').reverse().join(''), 2 );
or just (if you would want to convert the starting string, without the reversal):
var b = parseInt( a, 2 );
Just call parseInt with a different radix, in this case use 2 for binary.
var a = parseInt("01001011", 2);
// a === 75
parseInt attempts to figure out the radix itself when you don't explicitly specify it. From the Mozilla Developer Network:
If radix is undefined or 0, JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
In this case, it is crucial that you do specify the radix, as otherwise it may be interpreted as either a decimal or an octal number. As a rule of thumb, always specify the radix.
This will take a buffer hex and convert it to a binary str and back to the buffer hex.
NOTE: when I say a buffer hex, I mean a decimal value because when you iterate over a buffer and pull each item in the array, it gives you the decimal value (eg: 210, instead of d2).
var buffer - new Buffer([0, 210, 242]); // Node
// var arrayBuffer = new ArrayBuffer(3); // JavaScript
// var uint8 = new Uint8Array(arrayBuffer); // JavaScript/ 16Array, 32Array, etc
Need to be acquainted with buffers
You'll iterate over the buffer with a for(){} and inside you can do something like:
(210).toString(2); // '11010010'
(210).toString(16); // 'd2' (untested)
(210).toString(8); // (Octal-Digits representation)
parseInt((210).toString(2), 2); // 210
parseInt((210).toString(2), 2).toString(16); // 'd2'
Obviously, instead of using "(210).toString(2)" IN YOU FOR LOOP, you would use "(buffer[i]).toString(2)"
Endian Rep is up to you! :) (array.reverse())
Hope this helps!
PS. parseInt(('00000' + (210).toString(2).substring(5, 8)), 2); // 2
parseInt((210).toString(2).substring(5, 8), 2); // 2
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
How to parseInt a string with leading 0
document.write(parseInt("07"));
Produces "7"
document.write(parseInt("08"));
Produces "0"
This is producing problems for me (sorry for this babble, I have to or I can't submit the question). Anyone know why it's being stupid or if there is a better function?
If you argument begins with 0, it will be parsed as octal, and 08 is not a valid octal number. Provide a second argument 10 which specifies the radix - a base 10 number.
document.write(parseInt("08", 10));
use this modification
parseInt("08",10);
rules for parseInt(string, radix)
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
You want parseInt('08', 10) to tell it to parse as a decimal.
You just using input parameters of that function in a bit wrong way. Check this for more info. Basically :
The parseInt() function parses a string and returns an integer.
The radix parameter is used to specify which numeral system to be
used, for example, a radix of 16 (hexadecimal) indicates that the
number in the string should be parsed from a hexadecimal number to a
decimal number.
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is
deprecated
If the string begins with any other value, the radix is 10
(decimal)
So 07 and 08 is parsed into octal . That's why 07 is 7 and 08 is 0 (it is rounded to closest)
Try this :
parseInt('08', 10)
it will produce 8