Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Aula 2 Ex1 Format String

Como faço para imprimir os valores no formato monetário em java? Em C# fica assim (Console APP):

using System;

namespace Aula2Ex1
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal jan = 15000;
            decimal fev = 23000;
            decimal mar = 17000;
            decimal tri = jan + fev + mar;

            Console.WriteLine("Gastos Janeiro: {0:C}", jan);
            Console.WriteLine("Gastos Fevereiro: {0:C}", fev);
            Console.WriteLine("Gastos Março: {0:C}", mar);

            Console.WriteLine("Gastos trimestre: {0:C}", tri);
            Console.ReadKey();
        }
    }
}
1 resposta
solução!

Descobri pesquisando no google:

import java.text.NumberFormat;

public class Main {

    public static void main(String[] args) {
        double jan = 15000;
        double fev = 23000;
        double mar = 17000;

        double tri = jan + fev + mar;

        double med = tri / 3;

        NumberFormat nfMoeda = NumberFormat.getCurrencyInstance();

        System.out.println("Gastos e Despesas - Empresa");
        System.out.println("Janeiro: " + nfMoeda.format(jan));
        System.out.println("Fevereiro: " + nfMoeda.format(fev));
        System.out.println("Março: " + nfMoeda.format(mar));

        System.out.println("Trimestre: " + nfMoeda.format(tri));
        System.out.println("Média Trimestre: " + nfMoeda.format(med));
    }
}
output:
Gastos e Despesas - Empresa
Janeiro: R$ 15.000,00
Fevereiro: R$ 23.000,00
Março: R$ 17.000,00
Trimestre: R$ 55.000,00
Média Trimestre: R$ 18.333,33