2017-07-03 12 views
0

私のGDXプログラムでは、2つのボタンを持つオプションメニューを作成しました。そのうちの1つは音量を増加させ、1つは音量を下げます。ボタンをクリックするたびに0.1ずつ増減します。ここでは、コードです:Java float非論理出力

soundMButton = new ImageButton(drawableSoundM); 
soundMButton.addListener(new ClickListener(){ 
    @Override 
    public void clicked(InputEvent event, float x, float y) { 
     if (Constants.soundLevel > 0.0) { 
      Constants.soundLevel -= 0.1; 
      System.out.println(Constants.soundLevel); 
     } 
     click.play(1.0f * Constants.soundLevel); 
    } 
}); 

soundPButton = new ImageButton(drawableSoundP); 
soundPButton.addListener(new ClickListener(){ 
    @Override 
    public void clicked(InputEvent event, float x, float y) { 
     if (Constants.soundLevel < 1.0) { 
      Constants.soundLevel += 0.1; 
      System.out.println(Constants.soundLevel); 
     } 
     click.play(1.0f * Constants.soundLevel); 
    } 
}); 

は私の出力が、しかしれる

0.9 
0.79999995 
0.6999999 
0.5999999 
0.4999999 
0.39999992 
0.29999992 
0.19999993 
0.09999993 
-7.301569E-8 
それはこのようなものだと、なぜ

は誰もが知っていない0.9、0.8、0.7、など?

答えて

1

浮動小数点数および倍精度浮動小数点数は、精度エラーが発生します。 1桁の値に丸めてください。 From Here

DecimalFormat oneDigit = new DecimalFormat("#,##0.0"); 

... 

Constants.soundLevel = Double.valueOf(oneDigit.format(Constants.soundLevel)); 
関連する問題