0

有人知道为什么将字符串转换为整数时计算字段会返回错误吗?

var $prix = Max of Prix de vente; //Value is 2000.00
var $titre = All of Nom du produit;
var $nbr_heure = $titre[0].substring(0, 3).trim(); //Value is "25"
parseInt($nbr_heure)

parseInt($prix)返回 2000

我试过parseFloat()Number(). 似乎只要它试图转换一个字符串,它就不会处理。

我的静态解决方法

var $prix = Max of Prix de vente;
var $titre = All of Nom du produit;
var $nbr_heure = $titre[0].substring(0, 3).trim();
var $taux_horaire = 0;
//Conversion stupide manuelle
if($nbr_heure == "25")
  $taux_horaire = $prix / 25;
else if($nbr_heure == "50")
  $taux_horaire = $prix / 50;
else if($nbr_heure == "100")
  $taux_horaire = $prix / 100;
else
  $taux_horaire = 89;
$taux_horaire;

更多信息,如果你这样做:

$prix + parseInt($nbr_heure); //Same error

$prix + $nbr_heure; //Gives 200025  (String concatenation)

感谢您的任何反馈!

4

1 回答 1

0

您收到错误是因为 parseInt() 返回NaN而不是实际数字。或者因为您的子字符串调用失败。

如果您的$titre字段不包含值,您的子字符串调用将失败。因此,请务必检查您的计算是否使用 value 而不是undefined.

类似地NaN,如果您使用空字符串调用它,则 parseInt 将返回。

于 2014-08-27T18:25:22.517 回答