Convert decimal to int with Convert.ToInt32 - CSharp Language Basics

CSharp examples for Language Basics:Data Type Cast

Description

Convert decimal to int with Convert.ToInt32

Demo Code

using System;/*from w  ww .j  av a  2 s.c  o  m*/
class MassCalculator
{
   const decimal MassOfElectron = 9.0E-28m;
   public static void Main()
   {
      decimal totalMass;
      int electronAmount;
      Console.WriteLine("Enter number of electrons");
      electronAmount = Convert.ToInt32(Console.ReadLine());
      totalMass = MassOfElectron * electronAmount;
      Console.WriteLine("Mass of " + electronAmount + " electrons: " + totalMass + " grams");
   }
}

Result


Related Tutorials