3

Is there a way to suppress scientific notation in Haxe?

For example, if I have:

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

In targets such as html5, this outputs: 0.00007075

However, c++ targets output: 7.075e-05

Is there a way to control whether scientific notation is used for string format?

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80

2 Answers2

2

Credits to Hugh Sanderson of GameHaxe who answered this at a forum.

Global output mode for floats can be changed using an extern:

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);
  }
}
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
1

Perhaps you can switch to a different library to convert floats to strings which specifies the format. E.g., https://github.com/polygonal/printf/blob/master/src/de/polygonal/Printf.hx#L119

Chii
  • 14,540
  • 3
  • 37
  • 44
  • 1
    Franco Ponticelli also has a similar [thx.format](https://github.com/fponticelli/thx.format/blob/master/src/thx/format/NumberFormat.hx) at GitHub – Jason Sturges Mar 30 '17 at 18:51