Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by AwesomeFaceHD · Jan 22, 2014 at 11:00 PM · c#calculationnumbers

How can I input more than one digit numbers on my calculator I made?

I made a calculator and everything works fine, I just don't know how I would make it able to do more than one digit numbers. Can you please help me out? Here is the code:

 using UnityEngine;
 using System.Collections;
 
     public class Calculator : MonoBehaviour {
     
         string asmd = ""; //Addition, subtraction...
         int num1 = 0;
         int num2 = 0;
         int answer = 0;
         bool num1chosen = false;
     
         void OnGUI() {
             GUI.Box(new Rect(10, 10, 370, 315), "");
             GUI.Box(new Rect(15, 15, 80, 30), "" + num1);
             GUI.Box(new Rect(100, 15, 50, 30), "" + asmd);
             GUI.Box(new Rect(155, 15, 80, 30), "" + num2);
             GUI.Box(new Rect(240, 15, 50, 30), "=");
             GUI.Box(new Rect(295, 15, 80, 30), "" + answer);
             if(GUI.Button(new Rect(15, 50, 80, 50), "7")) {
                 if(num1chosen == false) {
                   num1 = 7;
                 }
                 else {
                   num2 = 7;
                 }
             }
             if(GUI.Button(new Rect(100, 50, 80, 50), "8")) {
                 if(num1chosen == false) {
                   num1 = 8;
                 }
                 else {
                   num2 = 8;
                 }
             }
             if(GUI.Button(new Rect(185, 50, 80, 50), "9")) {
                 if(num1chosen == false) {
                   num1 = 9;
                 }
                 else {
                   num2 = 9;
                 }
             }
             if(GUI.Button(new Rect(15, 105, 80, 50), "4")) {
                 if(num1chosen == false) {
                   num1 = 4;
                 }
                 else {
                   num2 = 4;
                 }
             }
             if(GUI.Button(new Rect(100, 105, 80, 50), "5")) {
                 if(num1chosen == false) {
                   num1 = 5;
                 }
                 else {
                   num2 = 5;
                 }
             }
             if(GUI.Button(new Rect(185, 105, 80, 50), "6")) {
                 if(num1chosen == false) {
                   num1 = 6;
                 }
                 else {
                   num2 = 6;
                 }
             }
             if(GUI.Button(new Rect(15, 160, 80, 50), "1")) {
                 if(num1chosen == false) {
                   num1 = 1;
                 }
                 else {
                   num2 = 1;
                 }
             }
             if(GUI.Button(new Rect(100, 160, 80, 50), "2")) {
                 if(num1chosen == false) {
                   num1 = 2;
                 }
                 else {
                   num2 = 2;
                 }
             }
             if(GUI.Button(new Rect(185, 160, 80, 50), "3")) {
                 if(num1chosen == false) {
                   num1 = 3;
                 }
                 else {
                   num2 = 3;
                 }
             }
             if(GUI.Button(new Rect(15, 215, 165, 50), "0")) {
                 if(num1chosen == false) {
                     num1 = 0;
                 }
                 else {
                     num2 = 0;
                 }
             }
             if(GUI.Button(new Rect(185, 215, 80, 50), "AC")) {
                 num1 = 0;
                 num2 = 0;
                 answer = 0;
                 asmd = "";
                 num1chosen = false;
             }
             if(GUI.Button(new Rect(15, 270, 360, 50), "=")) {
                 if(asmd == "+") {
                     answer = num1 + num2;
                 }
                 else if(asmd == "-") {
                     answer = num1 - num2;
                 }
                 else if(asmd == "*") {
                     answer = num1 * num2;
                 }
                 else if(asmd == "/") {
                     answer = num1 / num2;
                 }
             }
             if(GUI.Button(new Rect(270, 50, 105, 50), "+")) {
                 asmd = "+";
                 num1chosen = true;
             }
             if(GUI.Button(new Rect(270, 105, 105, 50), "-")) {
                 asmd = "-";
                 num1chosen = true;
             }
             if(GUI.Button(new Rect(270, 160, 105, 50), "*")) {
                 asmd = "*";
                 num1chosen = true;
             }
             if(GUI.Button(new Rect(270, 215, 105, 50), "/")) {
                 asmd = "/";
                 num1chosen = true;
             }
         }
     }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanobrain · Jan 23, 2014 at 12:07 AM 0
Share

Could you please expound on your question? I'm not quite sure what you mean by 'more than one digit numbers'.

avatar image AwesomeFaceHD · Jan 23, 2014 at 12:13 AM 0
Share

Like 7 is one digit, 77 is two digits.

avatar image RoDester · Jan 23, 2014 at 04:15 AM 0
Share

You should use arrays to store numbers.

avatar image Jamora · Jan 23, 2014 at 06:50 AM 0
Share

You need to think of a way to move all currently selected numbers left by one, so 52 -> 520, 253 -> 2530 etc. After having achieved this, you need to figure out how to replace that 0 with the digit the user has pressed...

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by ankush_Kushwaha · Jan 23, 2014 at 05:58 AM

Let say you have answere 737

then try this

 int _ans = 737; // put your answere here 
 int digit;
 int lastDigit;

 int i=0;
 while(Math.abs(_ans) >0){
      digit= _ans%10;
     _ans = _ans/10;


      digitArray[i]= digit;   // here you can collect digit into array or some other data structure
    i++;     // digits would be in reverse order
    if(_ans <10 ) _ans =0;
      print(digit);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Find angle between two gameobjects? 1 Answer

Random Number Generated within Array 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges