Java Double to String conversion without formatting

前端 未结 9 1331
余生分开走
余生分开走 2020-12-06 09:29

I have the number 654987. Its an ID in a database. I want to convert it to a string. The regular Double.ToString(value) makes it into scientific form, 6.54987E5. Something

9条回答
  •  难免孤独
    2020-12-06 09:56

    Use a fixed NumberFormat (specifically a DecimalFormat):

    double value = getValue();
    String str = new DecimalFormat("#").format(value);
    

    alternatively simply cast to int (or long if the range of values it too big):

    String str = String.valueOf((long) value);
    

    But then again: why do you have an integer value (i.e. a "whole" number) in a double variable in the first place?

提交回复
热议问题