为什么在以下情况下,nodejs中的isNaN函数返回 false ?
isNaN(''),isNaN('\n'),isNaN('\t')
这很奇怪。
有人有任何想法,因为我认为NaN代表的不是数字。
有人可以澄清
提前致谢!
答案 0 :(得分:14)
因为您没有传递数字,所以会将其转换为数字。所有这些都转换为0
0
而非NaN
Number('')
0
Number('\n')
0
Number('\t')
0
isNaN(0)
false
请注意,NaN
不代表"不代表JavaScript编号"。 In fact it's completely separate from JavaScript并且存在于支持IEEE-754浮动的所有语言中。
如果您想检查某些内容是否为javascript号,则检查为
if (typeof value === "number") {
}
答案 1 :(得分:4)
NaN是一个非常具体的东西:它是一个浮点值,根据IEEE754规范(Wikipedia article)设置了适当的NaN标志。
如果要检查字符串中是否包含数字值,可以执行parseFloat(str)
(MDN on parseFloat)。如果找不到任何有效的数字内容,或者在查找数字之前找到无效字符,则会返回NaN值。
因此,请尝试isNaN(parseFloat(str))
- 它会为我发布的所有三个示例提供true
。
答案 2 :(得分:3)
//java file
public class MainActivity_D6 extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Spinner spinner2, spinner1;
private String s_name, s_course;
//An ArrayList for Spinner Items
private ArrayList<String> students1;
private ArrayList<String> students2;
Button mBtnSave;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//JSON Array
private JSONArray result1, result2, result;
//TextViews to display details
private TextView textViewName1;
private TextView textViewName2;
private TextView textViewCourse;
private TextView textViewSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity_d);
//Initializing the ArrayList
students1 = new ArrayList<String>();
students2 = new ArrayList<String>();
//Initializing Spinner
//Adding an Item Selected Listener to our Spinner
//As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
mBtnSave = (Button) findViewById(R.id.button2);
mBtnSave.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String result = null;
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
s_name = spinner2.getSelectedItem().toString();
s_course = spinner1.getSelectedItem().toString();
nameValuePairs.add(new BasicNameValuePair("s_course",s_course));
nameValuePairs.add(new BasicNameValuePair("s_name",s_name));
StrictMode.setThreadPolicy(policy);
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.in/Spinner/insert_s2.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
Intent i = new Intent(getBaseContext(),DatabaseActivity.class);
startActivity(i);
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONObject json_data = new JSONObject(result);
CharSequence w= (CharSequence) json_data.get("re");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
});
/* mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// submitForm();
}
});*/
//Initializing TextViews
textViewName1 = (TextView) findViewById(R.id.textViewName1);
textViewName2 = (TextView) findViewById(R.id.textViewName2);
//This method will fetch the data from the URL
getData1();
getData2();
}
/* private void submitForm() {
s_name = spinner2.getSelectedItem().toString();
// s_name="aaa";
s_course = spinner1.getSelectedItem().toString();
// s_course="bbb";
Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
new InsertActivity(this).execute(s_course, s_name);
}*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
/* switch (parent.getId()){
case R.id.spinner1:
s_course= parent.getItemAtPosition(position).toString();
// getData1();
// showToast("Spinner1: position=" + position);
break;
case R.id.spinner2:
s_name= parent.getItemAtPosition(position).toString();
// getData2();
break;
}*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
/* private String getName(int position){
String name="";
try {
//Getting object of given index
JSONObject json = result.getJSONObject(position);
//Fetching name from that object
name = json.getString(Config.TAG_NAME);
} catch (JSONException e) {
e.printStackTrace();
}
//Returning the name
return name;
}
private String getCourse(int position){
String course="";
try {
JSONObject json = result.getJSONObject(position);
course = json.getString(Config.TAG_COURSE);
} catch (JSONException e) {
e.printStackTrace();
}
return course;
}*/
private void getData1() {
//Creating a string request
StringRequest stringRequest1 = new StringRequest(Config.DATA_URL1,
new Response.Listener<String>() {
@Override
public void onResponse(String response1) {
JSONObject j1 = null;
try {
//Parsing the fetched Json String to JSON Object
j1 = new JSONObject(response1);
//Storing the Array of JSON String to our JSON Array
result1 = j1.getJSONArray(Config.JSON_ARRAY1);
//Calling method getStudents to get the students from the JSON Array
getStudents1(result1);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error1) {
}
});
//Creating a request queue
RequestQueue requestQueue1 = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue1.add(stringRequest1);
}
private void getStudents1(JSONArray j1) {
//Traversing through all the items in the json array
for (int i = 0; i < j1.length(); i++) {
try {
//Getting json object
JSONObject json1 = j1.getJSONObject(i);
//Adding the name of the student to array list
students1.add(json1.getString(Config.TAG_COURSE));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner1.setAdapter(new ArrayAdapter<String>(MainActivity_D6.this, android.R.layout.simple_spinner_dropdown_item, students1));
}
private void getData2() {
//Creating a string request
StringRequest stringRequest2 = new StringRequest(Config.DATA_URL2,
new Response.Listener<String>() {
@Override
public void onResponse(String response2) {
JSONObject j2 = null;
try {
//Parsing the fetched Json String to JSON Object
j2 = new JSONObject(response2);
//Storing the Array of JSON String to our JSON Array
result = j2.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getStudents2(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error1) {
}
});
//Creating a request queue
RequestQueue requestQueue2 = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue2.add(stringRequest2);
}
private void getStudents2(JSONArray j2) {
//Traversing through all the items in the json array
for (int i = 0; i < j2.length(); i++) {
try {
//Getting json object
JSONObject json2 = j2.getJSONObject(i);
//Adding the name of the student to array list
students2.add(json2.getString(Config.TAG_USERNAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner2.setAdapter(new ArrayAdapter<String>(MainActivity_D6.this, android.R.layout.simple_spinner_dropdown_item, students2));
}
}
旨在帮助检测数学上未定义的内容&#39; - 例如isNan()
-
0/0
你在这个问题中提到的关于如何检测数字的其他建议是可靠的。
答案 3 :(得分:0)
isNaN是一个“不是数字”函数,当没有数字作为参数赋予它时返回true,当给出数字时返回false