【问题标题】:Suppress scientific notation in Haxe across targets在 Haxe 中跨目标抑制科学记数法
【发布时间】:2017-03-24 21:43:20
【问题描述】:

有没有办法在 Haxe 中抑制科学记数法?

例如,如果我有:

var f:Float = 0.00007075;
var s:String = Std.string(f);

在 html5 等目标中,输出:0.00007075

但是,c++ 的目标是输出:7.075e-05

有没有办法控制字符串格式是否使用科学计数法?

【问题讨论】:

  • 您可能想将您的答案标记为已接受? :)

标签: string-formatting haxe scientific-notation openfl


【解决方案1】:

感谢 GameHaxe 的 Hugh Sanderson 在论坛上回答了这个问题。

浮点数的全局输出模式可以使用外部变量来改变:

class Test
{
  @:native("__hxcpp_set_float_format") @:extern
  static function setFloatFormat(format:String):Void { }

  public static function main()
  {
    trace(0.00005);
    trace(5e20);
    setFloatFormat("%.12f");
    trace(0.00005);
    trace(5e20);
  }
}

【讨论】:

    【解决方案2】:

    也许您可以切换到不同的库以将浮点数转换为指定格式的字符串。例如,https://github.com/polygonal/printf/blob/master/src/de/polygonal/Printf.hx#L119

    【讨论】:

    • Franco Ponticelli 在 GitHub 上也有类似的 thx.format
    猜你喜欢
    • 2020-05-31
    • 2021-06-17
    • 1970-01-01
    • 2013-07-18
    • 2017-03-13
    • 2021-09-30
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多