parseint

parseInt() parses number literals with exponent incorrectly

╄→尐↘猪︶ㄣ 提交于 2019-12-01 16:17:00
I have just observed that the parseInt function doesn't take care about the decimals in case of integers (numbers containing the e character). Let's take an example: -3.67394039744206e-15 > parseInt(-3.67394039744206e-15) -3 > -3.67394039744206e-15.toFixed(19) -3.6739e-15 > -3.67394039744206e-15.toFixed(2) -0 > Math.round(-3.67394039744206e-15) 0 I expected that the parseInt will also return 0 . What's going on at lower level? Why does parseInt return 3 in this case (some snippets from the source code would be appreciated)? In this example I'm using node v0.12.1 , but I expect same to happen

Using JavaScript's parseInt at end of string

无人久伴 提交于 2019-12-01 15:54:40
问题 I know that parseInt(myString, 10) // "Never forget the radix" will return a number if the first characters in the string are numerical, but how can I do this in JavaScript if I have a string like "column5" and want to increment it to the next one ("column6")? The number of digits at the end of the string is variable. 回答1: parseInt("column5".slice(-1), 10); You can use -1 or -2 for one to two digit numbers, respectively. If you want to specify any length, you can use the following to return

parseInt() parses number literals with exponent incorrectly

僤鯓⒐⒋嵵緔 提交于 2019-12-01 14:18:40
问题 I have just observed that the parseInt function doesn't take care about the decimals in case of integers (numbers containing the e character). Let's take an example: -3.67394039744206e-15 > parseInt(-3.67394039744206e-15) -3 > -3.67394039744206e-15.toFixed(19) -3.6739e-15 > -3.67394039744206e-15.toFixed(2) -0 > Math.round(-3.67394039744206e-15) 0 I expected that the parseInt will also return 0 . What's going on at lower level? Why does parseInt return 3 in this case (some snippets from the

Why does using Array.map(parseInt) on an array of strings produce different results

好久不见. 提交于 2019-12-01 10:35:45
I was watching a talk on destroy all software title The Birth and Death of Javascript during the talk Gary Bernhardt pointed out a JavaScript quirky features, were given an array of integer strings, javascript var a = ['10','10','10','10'] console.log(a.map(parseInt)); // [10, NaN, 2, 3, 4] Array.map() takes a function and returns a new array with the result of applying this function on each operator. I found the behavior incredibly bizarre at first, doesn't parseInt parse a number to an integer? why would it be NaN ? and then not 10!! JavaScript is often the subject of parody, for its

Why does using Array.map(parseInt) on an array of strings produce different results

天大地大妈咪最大 提交于 2019-12-01 08:12:48
问题 I was watching a talk on destroy all software title The Birth and Death of Javascript during the talk Gary Bernhardt pointed out a JavaScript quirky features, were given an array of integer strings, javascript var a = ['10','10','10','10'] console.log(a.map(parseInt)); // [10, NaN, 2, 3, 4] Array.map() takes a function and returns a new array with the result of applying this function on each operator. I found the behavior incredibly bizarre at first, doesn't parseInt parse a number to an

In Java, how do I check if input is a number?

情到浓时终转凉″ 提交于 2019-12-01 06:41:15
I am making a simple program that lets you add the results of a race, and how many seconds they used to finish. So to enter the time, I did this: int time = Integer.parseInt(JOptionPane.showInputDialog("Enter seconds")); So my question is, how can I display an error message to the user if he enters something other than a positive number? Like a MessageDialog that will give you the error until you enter a number. int time; try { time = Integer.parseInt(JOptionPane.showInputDialog("Enter seconds")); } catch (NumberFormatException e) { //error } Integer.parseInt will throw a NumberFormatException

javascript, parseInt behavior when passing in a float number

泄露秘密 提交于 2019-12-01 05:14:52
I have the following two parseInt() and I am not quite sure why they gave me different results: alert(parseInt(0.00001)) shows 0; alert(parseInt(0.00000001)) shows 1 My guess is that since parseInt needs string parameter, it treats 0.00001 as ""+0.00001 which is "0.00001" , therefore, the first alert will show 0 after parseInt. For the second statement, ""+0.00000001 will be "1e-8" , whose parseInt will be 1 . Am I correct? Thanks Barmar I believe you are correct. parseInt(0.00001) == parseInt(String(0.00001)) == parseInt('0.00001') ==> 0 parseInt(0.00000001) == parseInt(String(0.00000001)) ==

In Java, how do I check if input is a number?

冷暖自知 提交于 2019-12-01 05:02:35
问题 I am making a simple program that lets you add the results of a race, and how many seconds they used to finish. So to enter the time, I did this: int time = Integer.parseInt(JOptionPane.showInputDialog("Enter seconds")); So my question is, how can I display an error message to the user if he enters something other than a positive number? Like a MessageDialog that will give you the error until you enter a number. 回答1: int time; try { time = Integer.parseInt(JOptionPane.showInputDialog("Enter

微信小程序开发记录(七)新版授权登录的实现

安稳与你 提交于 2019-11-30 17:56:50
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/wh_xmy/article/details/86475859 背景:由于微信官方修改了 getUserInfo、authorize 接口,无法弹出授权窗口,所以现在无法实现一进入微信小程序就弹出授权窗口,只能通过 button 去触发,这样就需要我们加一个授权页面去搞定了。 前提:微信授权登录仅适用于使用微信用户信息的用户,如果自己的项目有完善的用户信息(一套式开发项目),可不使用微信用户信息;如果仅为微信小程序,则要授权、获取用户信息。 实现思路:自己写一个微信授权登录页面让用户实现点击的功能,也就是实现了通过 button 组件去触发 getUserInof 接口。在用户进入微 信小程序的时候,判断用户是否授权了,如果没有授权的话就显示“授权页面”,让用户去执行授权的操作。如果已经授了,则直接跳过这个页面,进入首页。 授权页面的代码: authorize.wxml <!--pages/authorize/authorize.wxml--> <view class="box" wx:if="{{canIUse}}"> <button class='bottom' open-type="getUserInfo" lang=

Integer的parseInt和valueOf的区别

一世执手 提交于 2019-11-30 11:59:13
先来看一下下面这段代码 String s = "1"; System.out.println(Integer.valueOf(s)); System.out.println(Integer.parseInt(s)); 输出结果是什么?没错,一样都是1。两个方法都可以把数字类型字符串转成int类型整数,但是这两个方法还是有一点区别的, valueOf(String s) 方法调用了 parseInt(String s, int radix) 方法,而 parseInt(String s, int radix) 方法返回值是一个int类型的值,之后又调用了 valueOf(int i) 方法将int进行了装箱返回包装类型Integer。 所以如果你不需要返回包装类型,可以直接调用 parseInt(String s) 方法,效率更高。 下面这段是 valueOf(String s) 方法的源码 public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } 可以看到调用parseInt方法的时候还传了一个int类型参数radix,这个参数表示进制,默认使用十进制进行转换。下面是方法的源码,我标注了一些注释。 public