【问题标题】:Error parsing string to int in Podio在 Podio 中将字符串解析为 int 时出错
【发布时间】:2014-08-26 20:04:43
【问题描述】:

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

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)

感谢您的任何反馈!

【问题讨论】:

  • 错误信息是什么?
  • Le résultat n'est pas un nombre valide 表示The result is not a valid number。我还在计算字段中发现了一个错误,如果第一个结果类型在保存时是一个字符串,那么在修改时它将永远期待相同的类型,除非您删除该字段并创建一个新字段。

标签: string podio


【解决方案1】:

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

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

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

【讨论】:

  • 删除单位(heures)也不会改变任何东西。 (如果它包含在 parseInt 调用中)根据您的回答,返回的类型可能不是本机 javascript 而是 PodioCustomClass 吗?
  • 在您的第二个字段中尝试用parseInt("25"); 替换parseInt($nbr_heure); - 这应该可以。我自己在计算字段中使用 parseInt(),但您必须确保输入是您认为的那样。如果您的 $titre 变量最终为空,您将收到错误。因此,您必须检查 parseInt() 的输入是否有效。
  • 你说得对,它确实适用于手动字符串。但是在我的屏幕截图中,如果它在字符串操作后显示“25”的结果,那还能是什么?为什么parseInt() 不起作用?该屏幕截图是相同的字段,我让它显示结果“25”,然后立即尝试使用parseInt()。抱歉拖了这么久,我很难理解这一点。
猜你喜欢
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
相关资源
最近更新 更多