VB.Net/Development/Math Function

Материал из VB Эксперт
Перейти к: навигация, поиск

Acos and Cos

Imports System
Imports System.Data
Imports System.Diagnostics
public class MainClass
   Shared Sub Main()
        Console.WriteLine(System.Math.Acos(0))
        Console.WriteLine(System.Math.Cos(10))
        
   End Sub
End Class


Asin and Sin

Imports System
Imports System.Data
Imports System.Diagnostics
public class MainClass
   Shared Sub Main()
        Console.WriteLine(System.Math.Asin(0))
        Console.WriteLine(System.Math.Sin(10))
        
   End Sub
End Class


Atan and Tan

Imports System
public class MainClass
   Shared Sub Main()
        Console.WriteLine(System.Math.Atan(10))
        Console.WriteLine(System.Math.Tan(10))
        
   End Sub
End Class


Calculating compound interest

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim amount, principal As Decimal " dollar amounts
      Dim rate As Double               " interest rate
      Dim year As Integer              " year counter
      Dim output As String             " amount after each year
      principal = 1000
      rate = 0.05
      output = "Year" & vbTab & "Amount on deposit" & vbCrLf
      " calculate amount after each year
      For year = 1 To 10
         amount = principal * (1 + rate) ^ year
         output &= year & vbTab & _
            String.Format("{0:C}", amount) & vbCrLf
      Next
      " display output
      Console.WriteLine("Compound Interest" & output)
    End Sub
    
End Class


Math class: Square root, absolute value, Pi and Power

Imports System.IO
Module Module1
    Sub Main()
        Console.WriteLine("Absolute value of -1 is " & Math.Abs(-1))
        Console.WriteLine("Square Root of 144 is " & Math.Sqrt(144))
        Console.WriteLine("Value for PI is " & Math.PI)
        Console.WriteLine("10 raised to the power of 2 is " & Math.Pow(10, 2))
        
    End Sub
End Module


Math.Max Demo

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Console.WriteLine(Math.Max(Math.Max(1, 2), 3))
    End Sub
End Class


Math: Min, Max, Exp, Ceil, Round and Log10

Module Module1
    Sub Main()
        Console.WriteLine("Ceiling(1.1), Ceiling(0.9) = " & Math.Ceiling(1.1) & " " & Math.Ceiling(0.9))
        Console.WriteLine("Exp(5) = " & Math.Exp(5))
        Console.WriteLine("Log10(100) = " & Math.Log10(100))
        Console.WriteLine("Max(1001, 100) = " & Math.Max(1001, 100))
        Console.WriteLine("Min(1001, 100) = " & Math.Min(1001, 100))
        Console.WriteLine("Floor(1.1), Floor(0.9) = " & Math.Floor(1.1) & " " & Math.Floor(0.9))
        Console.WriteLine("Round(1.1), Round(0.9) = " & Math.Round(1.1) & " " & Math.Round(0.9))
    End Sub
End Module


Math: square root

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      " Calculate square root of 2
      Dim root As Double = Math.Sqrt(2)
      Console.WriteLine("The square root of 2 is " & root, _
         "The Square Root of 2")
    End Sub
    
End Class


Power in VB

Imports System
public class MainClass
    Shared Sub Main()

       Dim value As Integer = 5
       Dim power As Integer = 4
       Console.WriteLine("{0} to the {1}th power is {2}", _
          value, power, value ^ power)
    End Sub
End Class


Power ^ in VB.net

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim i As Integer " counter
      Console.WriteLine("Number" & vbTab & "Square" & vbCrLf)
      " square numbers from 1 to 10
      For i = 1 To 10
         Console.WriteLine(i & vbTab & Square(i))
      Next
    End Sub
   Shared Function Square(ByVal y As Integer) As Integer
      Return y ^ 2
   End Function 
 
End Class


Rnd Demo

Imports System
Imports System.Data
Imports System.Diagnostics
public class MainClass
   Shared Sub Main()
        Console.WriteLine(Rnd())
   End Sub
End Class


Round in Math

Imports System
Imports System.Data
Imports System.Diagnostics
public class MainClass
   Shared Sub Main()
        Console.WriteLine(System.Math.Round(1.4))
   End Sub
End Class


Sign in Math

Imports System
public class MainClass
   Shared Sub Main()
        Console.WriteLine(System.Math.Sign(-1))
        Console.WriteLine(System.Math.Sign(0))
        Console.WriteLine(System.Math.Sign(1))
   End Sub
End Class


Square Root in Math package

Imports System
Imports System.Data
Imports System.Diagnostics
public class MainClass
   Shared Sub Main()
        Console.WriteLine(System.Math.Sqrt(4))
   End Sub
End Class


To the Power

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
       Dim value As Integer = 5
       Dim power As Integer = 4
       Console.WriteLine("{0} to the {1}th power is {2}", _
          value, power, value ^ power)
    End Sub
End Class