【问题标题】:Retrieve decimals only from a variable仅从变量中检索小数
【发布时间】:2016-06-08 19:26:14
【问题描述】:

我正在制作一个距离/时间使用计算器,我试图将整个小时和分钟分开。

代码:

if (hours >= 1)
{
    minutes = Number((hours-int.(hours))*60);
    timeTxt.text = String(int.(hours)+" hours & "+(minutes)+" minutes");
}
else
{
    timeTxt.text = String((hours*60).toFixed(0)+" minutes");
}

"else" 正在工作并且只打印分钟。但我无法让第一个 if 语句起作用。正在尝试打印“x hours & x minutes”。

在输出中获取此消息:

TypeError:错误 #1123:类型类 int 不支持筛选运算符。 在 Rutekalkulator2_fla::MainTimeline/calculate()

【问题讨论】:

  • int.(hours) 应该是什么?如果您尝试将hours 转换为int,那么您需要((int) hours)
  • 尝试仅通过取小时(带小数)并仅通过使用 int(hours) 减去整个小时来检索分钟,如果可行,然后乘以 60。例如小时 = 3,73:分钟 =Number (3,73 - int(3,73))*60;
  • 使用 ((int) hours) 而不是 int.(hours) 似乎有效。谢谢!
  • 没问题。无需任何数学即可解决此问题的另一种方法是通过临时变量从hours 中生成string。然后你可以用indexOf检查是否存在,字符并提取两边的子字符串。
  • 您还应该检查将hours 转换为int 是否会舍入它(例如,3.7 会变成 4),这可能不是您的意图。

标签: string actionscript-3


【解决方案1】:

你不能这样做: (伪代码)

double tmp = math.floor(yournumber); 你的号码 = 你的号码 - tmp;

这将使您的数字只是小数部分。

除此之外,我不确定你的问题是什么。

【讨论】:

    【解决方案2】:

    将所有内容转换为分钟,然后尝试以下操作:

    timeTxt.text = String( totMinutes / 60 + ":" + totMinutes % 60);
    // output 06:36
    
    timeTxt.text = String( totMinutes / 60 + " hours and " + totMinutes % 60 + " minutes");
    // output 6 hours and 36 minutes
    

    我不精通 Actionscript,但如果模的工作方式与在其他语言中的工作方式相同,那么这应该适合你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多