How to save to type Numeric in Mysql

Asked

Viewed 107 times

1

Hello, I am using to save the price of a product in MYSQL the type Numeric(9,2) but in my system I am using Double and when printing it appears R $ 2.2 instead of R $ 2.20 what kind of data could use for it to pick all the decimal places?

//My Product Class with Getter and Setter Methods

public class Produto {
   private Double preco;

    public Double getPreco() {
        return preco;
    }

    public void setPreco(Double preco) {
        this.preco = preco;
    }

}

//Add Method

public void adiciona(Produto produto) {

        id_conexao = N.Conectar();
        String sql = "insert into produto (nome, preco, unidMedida, imgProduto) values (?,?,?,?)";

        try {

            //PreparedStatement para inserção
            stmt = id_conexao.prepareStatement(sql);

            //setar valores
            stmt.setString(1, produto.getNome());
            stmt.setDouble(2, produto.getPreco());
            stmt.setString(3, produto.getUnidMedida());
            stmt.setBytes(4, produto.getImgProduto());

            //executa
            stmt.execute();
            System.out.println("Salvo");


        } catch (SQLException e) {
            System.err.println("ERRO ao inserir produto - " + e);
        } finally {
            N.Desconectar();
        }

    }

//Creating object to send

Produto produto = new Produto();

produto.setNome(edtNome.getText().toString());                         
produto.setPreco(Double.parseDouble(edtPreco.getText().toString()));               
produto.setUnidMedida(txtUnidMedida.getText().toString());

//Printing object on Android interface

TextView nome = (TextView) rowView.findViewById(R.id.txtNome);
        TextView preco = (TextView) rowView.findViewById(R.id.txtPreco);
        TextView unidMedida = (TextView) rowView.findViewById(R.id.txtUnidMedida);
        //ImageView img_prod = (ImageView) rowView.findViewById(R.id.imgProduto);

        nome.setText(elementos.get(position).getNome());
        preco.setText(elementos.get(position).getPreco().toString());
        unidMedida.setText(elementos.get(position).getUnidMedida());
  • Where is the code that prints the value? the problem is not in this code you posted, the double is stored correctly in the bank, the problem is time to display it on the screen

  • put the code in place

1 answer

0


ok, somewhere in your code( you didn’t post ) you’re printing the double straight, output examples:

2.2
1.999999
3.0

You want something that leaves in the shape of money, Reais. then you must format Double to be printed this way has several ways to do this ex:

double preco = 2.20;
System.out.println( String.format( "%.2f", preco ) );

examples of output:

2.20
1.99
3.00
  • Ba solved my problem thanks.

  • could mark as correct answer? thank you.

  • I’m new here, how do I do it?

  • click on the correct sign that is on the side of my answer. as a 'tick' of certain

Browser other questions tagged

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