Pergunta

Eu tenho esse número 0,6044610142707825 e quero formatar para 600.Eu tentei o seguinte

new DecimalFormat("###.#")
    .format(temp)

Isso retornou 0,6.Por favor, ajude-me a encontrar o formato certo para fazer isso.

Foi útil?

Solução

Tentar formato decimal

// Fetch only the first rounded off digit after decimal.
String abc = new DecimalFormat("0.#").format(0.604461014);
// Converting to integer and multiply 1000
int myFinalNo =  (int) ( Double.parseDouble(abc) * 1000);
// Returns output 600

Observação:

String abc = new DecimalFormat("0.#").format(0.674461014);
int myFinalNo =  (int) ( Double.parseDouble(abc) * 1000);
// Returns output 700

Se você NÃO QUERO ARREDONDAMENTO, então use:

DecimalFormat df = new DecimalFormat("0.#");
df.setRoundingMode(RoundingMode.DOWN);
String abc = df.format(0.684461014);
int myFinalNo = (int) ( Double.parseDouble(abc) * 1000);
System.out.println ("abc="+abc + "** myFinalNo="+myFinalNo);

Outras dicas

isso pode resolver seu problema...

temp = 0.6044610142707825;

new DecimalFormat("###")
    .format(temp*1000); //converts to 604.4610142707825 and then to 604;

Você pode usar

String.format("%.2f", d), seu decimal será arredondado automaticamente.por 2 dígitos após o operador de pontos, onde D é o seu valor

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top