+ 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