function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SATZSATZ 

How to create an calculator in visualforce

Hi..

     can we create an simple calculater application in the apex page.if it s possible in saelsforce..

        help me..

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


Yes you can do this on VF page.

 

Try the below code as reference:


<apex:page controller="Calculator">
<apex:form >
<apex:inputText value="{!number1}"/>
<apex:inputText value="{!number2}"/>
<apex:inputText value="{!operator}"/>
<apex:commandButton value="ShowResult" action="{!calculation}">{!result}</apex:commandbutton>
</apex:form>
</apex:page>
//////////////// Controller /////////////////
public class Calculator
{
public integer number1{get;set;}
public integer number2{get;set;}
public string operator{get;set;}
public double result{get;set;}
public void calculation()
{
if(operator.contains('+'))
result=number1+number2;
if(operator.contains('-'))
result=number1-number2;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


Yes you can do this on VF page.

 

Try the below code as reference:


<apex:page controller="Calculator">
<apex:form >
<apex:inputText value="{!number1}"/>
<apex:inputText value="{!number2}"/>
<apex:inputText value="{!operator}"/>
<apex:commandButton value="ShowResult" action="{!calculation}">{!result}</apex:commandbutton>
</apex:form>
</apex:page>
//////////////// Controller /////////////////
public class Calculator
{
public integer number1{get;set;}
public integer number2{get;set;}
public string operator{get;set;}
public double result{get;set;}
public void calculation()
{
if(operator.contains('+'))
result=number1+number2;
if(operator.contains('-'))
result=number1-number2;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
SATZSATZ

hi..

thanks  ....

   

 

                Regards 

satz..

SATZSATZ

hi..

   What is the bulk insert? and why we are using bulk insert method to insert data in salesforce?

      

prangyaprangya

Can anyone tell me calculator program with single inputtext

HRUDANANDA SAHOO 6HRUDANANDA SAHOO 6
<-----visualforce page------>
<apex:page controller="test_two" showHeader="true">
  <apex:form >
  <apex:pageBlock title="Addition">
  A: <apex:inputText value="{!a}"/><br/><br/>
   B: <apex:inputText value="{!b}" /><br/><br/>
   <apex:pageBlockButtons >
   <apex:commandButton value="ADD" action="{!ab}" />
      </apex:pageBlockButtons>
  </apex:pageBlock>
  <apex:pageBlock >
  <b>Output is:<apex:outputText value="{!c}"></apex:outputText></b>
  </apex:pageBlock>
  <apex:pageBlock title="Multiplication">
  X: <apex:inputText value="{!X}"/><br/><br/>
   Y: <apex:inputText value="{!y}"/><br/><br/>
    <apex:pageBlockButtons >
   <apex:commandButton value="MULTIPLY" action="{!ac}" />
      </apex:pageBlockButtons>
   </apex:pageBlock> 
  <apex:pageBlock >
  <b>Output is:<apex:outputText value="{!z}"></apex:outputText></b>
  </apex:pageBlock>
  
  
  </apex:form>
</apex:page>

<-----controller-------->

public class test_two {

    public test_two(ApexPages.StandardController controller) {

    }
  public integer a {get;set;}
  public integer b {get;set;}
  public integer c {get;set;}
  
  public integer x {get;set;}
  public integer y {get;set;}
  public integer z {get;set;}
    
       
    public void ab(){
         c = a+b;
        
    }
    public integer ac(){
         z = x*y;
        return null;
        
    }

}
Fanindra mohan choudharyFanindra mohan choudhary
VISUALFORCE PAGE:

<apex:page controller="Calculator" showHeader="true">
  <apex:form >
  <apex:pageBlock title="CALCULATOR">
  NUMBER 1: <apex:inputText value="{!number1}"/><br/><br/>
   NUMBER 2: <apex:inputText value="{!number2}" /><br/><br/>
   <apex:pageBlockButtons >
   <apex:commandButton value="ADD" action="{!addition}" />
       <apex:commandButton value="SUB" action="{!subtraction}" />
       
         <apex:commandButton value="DIV" action="{!division}" />
       <apex:commandButton value="MULTIPLY" action="{!multiplication}" />
      </apex:pageBlockButtons>
 
  <b>Output is:<apex:outputText value="{!result}"></apex:outputText></b>
  </apex:pageBlock>
 
  
  
  </apex:form>
</apex:page>



APEX CLASS:


public class calculator{

   
  public integer number1 {get;set;}
  public integer number2 {get;set;}
  public integer result {get;set;}
  

       
    public void addition(){
         result = number1+number2;
        
    }
     public void subtraction(){
         result = number1-number2;
        
    }
     public void division(){
         result = number1/number2;
        
    }
    public void multiplication(){
         result = number1*number2;
      
        
    }

}
Chetan KapaniaChetan Kapania
Hi Fanindra Mohan Choudhary,

I have tried the code written by you and it is working perfectly. I am facing issue while writing test class for the same. Would you be able to share the test class?

Regards
Chetan Kapania 
Revan Kumar 8Revan Kumar 8
Visualforce Page for Salesforce1 and Salesforce Classic:-
<apex:page controller="Calculator"  showHeader="false" sidebar="false">
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <head>
        <style>
           .btn-primary{
              background:blue !important;
              width:70px;
          }
          input[type=checkbox] {
              /* Double-sized Checkboxes */
              -ms-transform: scale(2); /* IE */
              -moz-transform: scale(2); /* FF */
              -webkit-transform: scale(2); /* Safari and Chrome */
              -o-transform: scale(2); /* Opera */
              padding: 10px;
          }  
          .message ul,.messageText{
             color:red;
          } 
          .pbBody{
              overflow:auto;
          }
          .apexp{
              width:360px !important;
          }
                  
     </style>   
     <script>
     
     </script> 
    </head>
 <apex:form id="form">
     <apex:pageBlock title="Calculator">
         <apex:pageMessages ></apex:pageMessages>
         Enter First Number<apex:inputText title="Enter Number" label="Enter Number" value="{!Int1}" styleClass="form-control" html-placeholder="Enter value"/> 
         Enter Second Number<apex:inputText title="Enter Number" label="Enter Number" value="{!Int2}" styleClass="form-control" html-placeholder="Enter value"/>
         Ouput Number <apex:inputText title="Enter Number" label="Enter Number" value="{!Int3}" html-readonly="true" id="counter" styleClass="form-control"  rendered="{!myValue1==null}" html-placeholder="Output value"/>
          <apex:inputText title="Enter Number" label="Enter Number" value="{!myValue1}" html-readonly="true" id="counter1" styleClass="form-control" rendered="{!myValue1!=null}"/>
         Select Operator:-&nbsp;&nbsp;<apex:selectList size="1" value="{!myValue}" multiselect="false" label="Select Operators" styleClass="form-control">
            <apex:selectOptions value="{!Items}"/>
        </apex:selectList><br/>
       <center> <apex:commandButton value="Calculate" action="{!calculate}" styleclass="btn btn-default"/> </center>
     </apex:pageBlock>
 </apex:form>
</apex:page>
Apex Class:-
public class Calculator {
   public Decimal Int1 {get;set;}
   public Decimal Int2 {get;set;}
   public Decimal Int3 {get;set;}
   public String myValue {get;set;}
   public String myValue1 {get;set;}
   public integer addition (Integer a,integer b) {
       return a+b;     
   }
   public lead leadmethod (String lName) {
      Lead objLead = new Lead () ;
      objLead.LastName = lName;
      objLead.Company = 'PersiWipro';
      insert objLead ;
      return objLead;
  }
  public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('',''));
        options.add(new SelectOption('+','+'));
        options.add(new SelectOption('-','-'));
        options.add(new SelectOption('*','*'));
        options.add(new SelectOption('/','/'));
        options.add(new SelectOption('X2','X2'));
        return options;
  }
  public Calculator () {
      
  }
  public void calculate () {
      
      if(myValue == null || myValue =='') {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please select operator'));myValue1 = null;
      }
      else {
          if(myValue.equals('+')) {
              Int3 = Int1 + Int2; myValue1 = null;
          }  
          if(myValue.equals('+')) {
              Int3 = Int1 + Int2; myValue1 = null;
          }  
          if(myValue.equals('-')) {
              Int3 = Int1 - Int2; myValue1 = null;
          } 
          if(myValue.equals('*')) {
              Int3 = Int1 * Int2; myValue1 = null;
          } 
          if(myValue.equals('/')) {
              if(Int2==0) {
                  myValue1='cannot divide by zero';
              }else {
              Int3 = Int1 / Int2; myValue1 = null;
              } 
          } 
          if(myValue.equals('X2')) {
              Int3 = Int1 * Int1; myValue1 = null;
          } 
      }   
  }
  
}