Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Rounding To 2 Decimal Places

Rounding To 2 Decimal Places

Scheduled Pinned Locked Moved Visual Basic
questionsales
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10694570
    wrote on last edited by
    #1

    How do I code this to round my results to 2 decimal places? Module Module1 Dim itemQuantity As Integer = 0 Dim itemPrice As Decimal = 0 Dim totalSales As Decimal = 0 Dim stateName As String Dim taxRate As Decimal = 0 Dim taxAmount As Decimal = 0 Dim customerName As String Sub Main() Console.WriteLine("Please enter your name.") customerName = Console.ReadLine() Console.WriteLine("Please enter your state: NJ, FL, or NY") stateName = Console.ReadLine() Console.WriteLine("Please enter the number of items purchased.") itemQuantity = Console.ReadLine() Console.WriteLine("Please enter the price of the item.") itemPrice = Console.ReadLine() Call computeTotal() Call computeTax() Console.WriteLine("The total sales for " & customerName & " are " & totalSales & " .") Console.WriteLine("The tax amount is " & taxAmount & " based on a tax rate of " & taxRate & " .") Console.WriteLine("The total with tax is " & totalSales + taxAmount & " .") Console.ReadLine() End Sub Function computeTotal() As Decimal totalSales = itemQuantity * itemPrice Return totalSales End Function Function computeTax() As Decimal If stateName = "NJ" Then taxRate = 0.07 ElseIf stateName = "FL" Then taxRate = 0.06 ElseIf stateName = "NY" Then taxRate = 0.04 End If taxAmount = totalSales * taxRate Return taxAmount End Function End Module

    L 1 Reply Last reply
    0
    • U User 10694570

      How do I code this to round my results to 2 decimal places? Module Module1 Dim itemQuantity As Integer = 0 Dim itemPrice As Decimal = 0 Dim totalSales As Decimal = 0 Dim stateName As String Dim taxRate As Decimal = 0 Dim taxAmount As Decimal = 0 Dim customerName As String Sub Main() Console.WriteLine("Please enter your name.") customerName = Console.ReadLine() Console.WriteLine("Please enter your state: NJ, FL, or NY") stateName = Console.ReadLine() Console.WriteLine("Please enter the number of items purchased.") itemQuantity = Console.ReadLine() Console.WriteLine("Please enter the price of the item.") itemPrice = Console.ReadLine() Call computeTotal() Call computeTax() Console.WriteLine("The total sales for " & customerName & " are " & totalSales & " .") Console.WriteLine("The tax amount is " & taxAmount & " based on a tax rate of " & taxRate & " .") Console.WriteLine("The total with tax is " & totalSales + taxAmount & " .") Console.ReadLine() End Sub Function computeTotal() As Decimal totalSales = itemQuantity * itemPrice Return totalSales End Function Function computeTax() As Decimal If stateName = "NJ" Then taxRate = 0.07 ElseIf stateName = "FL" Then taxRate = 0.06 ElseIf stateName = "NY" Then taxRate = 0.04 End If taxAmount = totalSales * taxRate Return taxAmount End Function End Module

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use string.Format to format your values, rather than concatenation which prints all the digits.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups