|
|
|
Округление double
|
|||
|---|---|---|---|
|
#18+
Происходит нечто интересное и мне не понятное. Хочется округлить double до 2 знаков после точки: roundDouble(18.1221) = 18.12 class Rounder{ public static final double roundDouble(double d) { return Math.round (d * 100F ) / 100F ; } } class RounderTest extends TestCase{ public void testRoundDouble(){ double d = 18.2123213213213; System.out.println(Rounder.roundDouble(d)); System.out.println(Math.round (d * 100F ) / 100F ); } public static void main (String[] str){ } } На консоли: 18.209999084472656 18.21 Ответте мне неграмотному- почему? ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 23.05.2005, 07:25 |
|
||
|
Округление double
|
|||
|---|---|---|---|
|
#18+
Alexey TurnПроисходит нечто интересное и мне не понятное. Хочется округлить double до 2 знаков после точки: roundDouble(18.1221) = 18.12 public static final double roundDouble(double d) { return Math.round (d * 100F ) / 100F ; } } System.out.println(Rounder.roundDouble(d)); System.out.println(Math.round (d * 100F ) / 100F ); } imho перед выходом из метода roundDouble результат Math.round переводится в double согласно сигнатуре метода, вот и получается 18.209999084472656. а здесь: System.out.println(Math.round (d * 100F ) / 100F ) выводится сразу результат без переконвертации. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 23.05.2005, 09:06 |
|
||
|
|

start [/forum/topic.php?fid=59&msg=33079295&tid=2152337]: |
0ms |
get settings: |
10ms |
get forum list: |
15ms |
check forum access: |
3ms |
check topic access: |
3ms |
track hit: |
83ms |
get topic data: |
10ms |
get forum data: |
3ms |
get page messages: |
53ms |
get tp. blocked users: |
2ms |
| others: | 256ms |
| total: | 438ms |

| 0 / 0 |
