Appear 1.0k instead of number 1000 and so on

Asked

Viewed 141 times

1

How can I make it appear instead of 1000 appear 1,0k and so on?

1000 - 1,0k
2000 - 2,0k
10000 - 10,0k
100000 - 100,0k

and etc...

@Override
    public double getValor(String arg0) {
        Pessoa pessoa = Main.getPessoa();
        double valor = base.configvalor.getConfig().getConfigurationSection(pessoa.getNome()).getDouble("Valor");
        if (base.configvalor.getConfig().getConfigurationSection(pessoa.getNome()).getDouble("Valor") >= 1000) {
            double resultado = Math.ceil(valor/1000.0);
            return (resultado);
        }else {
            return valor;
        }
    }
  • 1

    Related question (not duplicate): https://answall.com/q/51047/132

1 answer

2


public class Main {

  public static void main(String args[]) {
    System.out.println(abreviarComK(1000));
    System.out.println(abreviarComK(2000));
    System.out.println(abreviarComK(10000));
    System.out.println(abreviarComK(300));

  }


  public static String abreviarComK(long numero) {

    if (numero < 1000) {
      return Long.toString(numero);
    } else {
      return (numero/1000.0 + "k").replace(".", ",");
    }

  }

}

Do not need replace if you are going to use a semicolon instead of a comma.

See working on Ideone.

  • How can I do this with the method: public double ?

  • I don’t understand. What public double method? I gave you a method that gets a long, which is what seems to match the input you are passing (comma-free numbers). If you want this method to receive a double, change the parameter type.

  • I put the code I tried, but I do not know how to show 1.2k and etc...

  • My answer contains the function abreviarComK that converts a number to that representation you asked for. Just call the function by passing the number and save the result in a String, just as it is being done in the main. See yourself working on ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.