If you found a problem with formatting float in GWT I am presenting a solution similar to described here in official libGDX wiki page (based on interfacing with platform specific code).
As you have probably noticed String.format() is not supported by GWT. We need to use alternative class NumberFormat from gwt client library, which is a simple replacement.
Firstly we need to edit our build.gradle file and add missing dependencies to html project.
1 2 |
compile "com.google.gwt:gwt-user:2.6.0" compile "com.google.web.bindery:requestfactory-server:2.6.0" |
Then we should refresh all gradle projects.
Next step is adding interface to core project.
1 2 3 |
public interface FloatFormatter { public String getFormattedString(float value); } |
Subsequently we create implementation for each platform.
html:
1 2 3 4 5 6 7 8 |
public class HtmlFloatFormatter implements FloatFormatter { @Override public String getFormattedString(float value) { NumberFormat format = NumberFormat.getFormat("#.#"); return format.format(value); } } |
desktop & android:
1 2 3 4 5 6 7 |
public class DesktopFloatFormatter implements FloatFormatter { @Override public String getFormattedString(float value) { return String.format("%.1f", value); } } |
1 2 3 4 5 6 7 |
public class AndroidFloatFormatter implements FloatFormatter { @Override public String getFormattedString(float value) { return String.format("%.1f", value); } } |
Then inside core project we need to add constructor to ApplicationListener.
1 2 3 4 5 6 7 |
public class LibgdxGame implements ApplicationListener { private final FloatFormatter floatFormatter; public LibgdxGame(FloatFormatter floatFormatter) { this.floatFormatter = floatFormatter; } } |
We also need to edit each starter class as following.
html:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class HtmlLauncher extends GwtApplication { @Override public GwtApplicationConfiguration getConfig () { return new GwtApplicationConfiguration(800, 480); } @Override public ApplicationListener getApplicationListener () { return new LibgdxGame(new HtmlFloatFormatter()); } } |
desktop:
1 2 3 4 5 6 7 8 |
public class DesktopLauncher { public static void main (String[] arg) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); config.width = 800; config.height = 480; new LwjglApplication(new LibgdxGame(new DesktopFloatFormatter()), config); } } |
android:
1 2 3 4 5 6 7 8 |
public class AndroidLauncher extends AndroidApplication { @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); initialize(new LibgdxGame(new AndroidFloatFormatter()), config); } } |
Now to recieve formatted float we just need to call:
1 |
String formattedFloat = floatFormatter.getFormattedString(floatValue); |