为什么这部分代码失败:
Integer.parseInt("11000000000000000000000000000000",2);
Exception in thread "main" java.lang.NumberFormatException: For input string: "11000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)
据我所知,Integer是32位值.上面代码中的0和1的数量是32.如果有31个代码可以工作.为什么会这样?
我刚注意到:
//IN CHROME JS CONSOLE
parseInt("03010123"); //prints: 3010123
//IN NODE.JS
parseInt("03010123"); //prints: 790611
Run Code Online (Sandbox Code Playgroud)
由于两者都基于V8,为什么同样的操作会产生不同的结果?
我刚刚观察到,在parseInt整数(包含e字符的数字)的情况下,函数不会处理小数.
我们来举个例子: -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
Run Code Online (Sandbox Code Playgroud)
我预计这parseInt也会回归0.在较低级别发生了什么?为什么在这种情况下parseInt返回3(源代码中的一些片段会受到赞赏)?
在这个例子中我正在使用node v0.12.1,但我希望在浏览器和其他JavaScript引擎中也会发生同样的情况.
我想知道是否有人把某些东西放在一起或者已经看到了与C#的JavaScript parseInt相当的东西.
具体来说,我希望采取如下字符串:
123abc4567890
Run Code Online (Sandbox Code Playgroud)
并仅返回第一个有效整数
123
Run Code Online (Sandbox Code Playgroud)
我有一个我用过的静态方法只返回数字:
public static int ParseInteger( object oItem )
{
string sItem = oItem.ToString();
sItem = Regex.Replace( sItem, @"([^\d])*", "" );
int iItem = 0;
Int32.TryParse( sItem, out iItem );
return iItem;
}
Run Code Online (Sandbox Code Playgroud)
以上将采取:
ParseInteger( "123abc4567890" );
Run Code Online (Sandbox Code Playgroud)
并把我还给我
1234567890
Run Code Online (Sandbox Code Playgroud)
我不确定是否可以使用正则表达式,或者是否有更好的方法来从字符串中获取第一个整数.
可能重复:
JavaScript的数学是否被破坏?
在Javascript中,我无法弄清楚为什么230/100*100返回229.99999999999997,而240/100*100返回240.
这也适用460, 920于......等等......
有什么解决方案吗?
我如何从文本字段中舍入一个值并将parseFloat结果包含在内?此应用程序基本上总结了单击时所有单选按钮的值,并在文本框中显示总和.
如果单选按钮值是一个整数,则下面的代码可以正常工作,但是如果我想在单选按钮上有浮点值,则总值将具有100.0000000679应该的时间100.任何提示将非常感谢.提前致谢.
function calcscore(){
var score = 0;
$(".calc:checked").each(function(){
score+=parseFloat($(this).val(),10);
});
$("input[name=openingsum]").val(score);
}
$().ready(function(){
$(".calc").change(function(){
calcscore();
});
});
Run Code Online (Sandbox Code Playgroud)
HTML代码:
<input class="calc" name="v2" type="radio" onclick="ver2(this);" value="1.6666666666666666666666666666667" />Yes
<input class="calc" name="v2" type="radio" onclick="ver2(this);" value="0" />No
Run Code Online (Sandbox Code Playgroud) 我正在尝试从另一个生成的文本文件中导入文本Activity.生成的文本文件由String ArrayList仅包含数字和由Android生成的其他随机文本组成.当我从文件中导入文本时,我正在使用a BufferedReader并将readLine()每个新数字转换为Integer ArrayList.我正从文本文件中删除任何非数字值,而在另一个Activity中生成的数字将被"\n"拆分.
我面临的问题是Android在加载时会崩溃Activity.我把原因缩小到了Integer.parseInt().
我的代码如下:
ArrayList<Integer> lines = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = new File(getFilesDir(), "test_file.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.readLine() != null) {
String text = (br.readLine()).replaceAll("[^0-9]+","").trim();
Integer number = Integer.parseInt(text);
lines.add(number);
}
} catch (IOException e) {
}
TextView tv = (TextView) findViewById(R.id.helptext);
int max = 0, min = 100;
double total …Run Code Online (Sandbox Code Playgroud) 长话短说,我试图验证手机领域.香港专业教育学院添加了isNaN和在现场parseInt检查," "但说
以下从未证实真实......我错过了什么?
if(isNaN(parseInt(phone))){
error.text("Sorry but this phone field requires numbers only");
return false;
} else {
return true;
}
Run Code Online (Sandbox Code Playgroud)
它总是失败......即使我在现场输入一个数字并提交,它也永远不会成立.我总是得到错误mssg.
编辑:我正在测试表单中的输入值,phone是字段的名称.
好吧......我有这个.txt文件(UTF-8)
4661,SOMETHING,3858884120607,24,24.09
4659,SOMETHING1,3858884120621,24,15.95
4660,SOMETHING2,3858884120614,24,19.58
Run Code Online (Sandbox Code Playgroud)
而这段代码
FileInputStream fis = new FileInputStream(new File(someTextFile.txt));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader in = new BufferedReader(isr);
int i = 0;
String line;
while((line = in.readLine()) != null) {
Pattern p = Pattern.compile(",");
String[] article = p.split(line);
// I don't know why but when a first line starts with
// an integer - article[0] (which in .txt file is 4661)
// becomes someWeirdCharacter4661 so I need to trim it
// *weird character is like |=>| …Run Code Online (Sandbox Code Playgroud) 我知道
parseInt(myString, 10) // "Never forget the radix"
Run Code Online (Sandbox Code Playgroud)
如果字符串中的第一个字符是数字,则会返回一个数字,但如果我有一个像"column5"这样的字符串并希望将其递增到下一个字符串("column6"),我怎样才能在JavaScript中执行此操作?
字符串末尾的位数是可变的.