Advertisement
NestaRama

Invoice tipe data double tidak akurat

Aug 29th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.84 KB | None | 0 0
  1. package com.bahasaJava.invoice;
  2.  
  3. import java.util.Scanner;
  4. import java.text.NumberFormat;
  5.  
  6. public class InvoiceDouble{
  7.     public static void main(String[] args){
  8.        
  9.         // Membuat objek Scanner object dan memulai while loop
  10.         Scanner input = new Scanner(System.in);
  11.         String pilihan = "y";
  12.        
  13.         //inisialisasi awal total invoice
  14.         double total = 0.0;
  15.         while (pilihan.equalsIgnoreCase("y")){
  16.             // Memperoleh input subtotal dari user
  17.             System.out.print("Masukkan jumlah subtotal: ");
  18.             double subtotal = input.nextDouble();
  19.             // Menghitung hasil
  20.             double persentaseDiskon = 0.0;
  21.             if (subtotal >= 100)
  22.                 persentaseDiskon = 0.1;
  23.             else
  24.                 persentaseDiskon = 0.0;
  25.            
  26.             double jumlahDiskon = subtotal * persentaseDiskon;
  27.             double totalSebelumPajak = subtotal - jumlahDiskon;
  28.             double pajakPenjualan = totalSebelumPajak * 0.05;
  29.             double totalAwal = totalSebelumPajak + pajakPenjualan;
  30.                    total += totalAwal;
  31.                
  32.            
  33.            
  34.             // Memformat dan menampilkan hasil
  35.             NumberFormat currency = NumberFormat.getCurrencyInstance();
  36.             NumberFormat percent = NumberFormat.getPercentInstance();
  37.            
  38.             String pesan = "Persentase Diskon: " + percent.format(persentaseDiskon) + "\n"
  39.             + "Jumlah Diskon: " + currency.format(jumlahDiskon) + "\n"
  40.                     + "Total sebelum pajak: " + currency.format(totalSebelumPajak) + "\n"
  41.             + "Pajak penjualan: " + currency.format(pajakPenjualan) + "\n"
  42.                     + "Sub total: " + currency.format(totalAwal) + "\n";
  43.            
  44.             System.out.println(pesan);
  45.            
  46.             // Memberikan pilihan apakah user akan melanjutkan atau tidak
  47.             System.out.print("Lanjutkan? (y/n): ");
  48.             pilihan = input.next();
  49.             System.out.println();
  50.            
  51.             }
  52.            
  53.             //Menampilkan total invoice
  54.             NumberFormat currency = NumberFormat.getCurrencyInstance();
  55.             System.out.println("Total invoice adalah: " + currency.format(total));
  56.         }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement