试图在javascript中计算简单的折扣和销售税

时间:2018-01-31 22:35:04

标签: javascript sales

我只是想输入一个数字,然后再加10%,然后拿出总额和.08%的税。你能帮我弄清楚我做错了什么。我确定这是一个简单的事情,我忽视了,但我是新手,无法弄清楚是什么问题。

__LINE__

3 个答案:

答案 0 :(得分:2)

我记录了'费用'在我以值100

开头的代码中
package com.androiddeft.navigationdrawer.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.androiddeft.navigationdrawer.MyListAdapter;
import com.androiddeft.navigationdrawer.R;
import com.androiddeft.navigationdrawer.Employee;
import com.androiddeft.navigationdrawer.DatabaseHelper;
import com.androiddeft.navigationdrawer.constants.NavigationDrawerConstants;

import java.util.List;


public class CustomersFragment extends Fragment {

    private Button btnSubmit;
    private Button btnCancel;
    private ListView listView;
    private MyListAdapter adapter;
    private EditText edtSearch, edtName, edtPhone, edtAddress;
    private int seletcedEmployeeId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(NavigationDrawerConstants.TAG_CUSTOMERS);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        // Create the view from XML layout.
        final View view = inflater.inflate(R.layout.fragment_customers, null);
        // Perform additional configuration on layout components here.
        Button btnAdd = (Button) view.findViewById(R.id.btn_add);
        Button btnShowSearch = (Button) view.findViewById(R.id.btn_search);
        Button btnCloseSearch = (Button) view.findViewById(R.id.btn_close_search);
        Button btnDoSearch = (Button) view.findViewById(R.id.btn_do_search);
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        btnSubmit = (Button) view.findViewById(R.id.btn_submit);

        edtSearch = (EditText) view.findViewById(R.id.edt_search);
        edtName = (EditText) view.findViewById(R.id.edt_employee_name);
        edtPhone = (EditText) view.findViewById(R.id.edt_employee_phone);
        edtAddress = (EditText) view.findViewById(R.id.edt_employee_address);

        btnAdd.setOnClickListener(onAddClicked);
        btnShowSearch.setOnClickListener(onShowSearchClicked);
        btnCloseSearch.setOnClickListener(onCloseSearchClicked);
        btnDoSearch.setOnClickListener(onDoSearchClicked);
        btnCancel.setOnClickListener(onCancelbuttonClicked);

        listView = (ListView) view.findViewById(R.id.listView);
        refreshList();
        return view;
    }
    private void refreshList() {
        List<Employee> employees = new DatabaseHelper(this).getListOfEmployees();
        adapter = new MyListAdapter(this, employees);
        listView.setAdapter(adapter);
    }

    public void editEmployee(Employee employee) {
        findViewById(R.id.lin_add_search).setVisibility(View.GONE);
        findViewById(R.id.add_edit_employee).setVisibility(View.VISIBLE);
        btnSubmit.setOnClickListener(onEditEmployeeSubmit);
        edtName.setText(employee.getName());
        edtPhone.setText(employee.getPhone());
        edtAddress.setText(employee.getAddress());
        seletcedEmployeeId = employee.getId();
    }

    private View.OnClickListener onAddClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            findViewById(R.id.lin_add_search).setVisibility(View.GONE);
            findViewById(R.id.add_edit_employee).setVisibility(View.VISIBLE);
            btnSubmit.setOnClickListener(onNewEmployeeSubmit);
        }
    };

    private View.OnClickListener onShowSearchClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            findViewById(R.id.lin_add_search).setVisibility(View.GONE);
            findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
        }
    };

    private View.OnClickListener onCloseSearchClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            findViewById(R.id.lin_add_search).setVisibility(View.VISIBLE);
            findViewById(R.id.search_bar).setVisibility(View.GONE);
            edtSearch.setText("");
            refreshList();
        }
    };

    private View.OnClickListener onDoSearchClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String input = edtSearch.getText().toString();
            if (input.length() == 0) {
                Toast.makeText(getActivity(), "لطفا قسمتی از نام کارمند را بنویسید", Toast.LENGTH_SHORT).show();
                return;
            }

            List<Employee> employees = new DatabaseHelper(getActivity()).searchByName(input);
            adapter = new MyListAdapter(getActivity(), employees);
            listView.setAdapter(adapter);
            Toast.makeText(getActivity(), employees.size() + " کارمند پیدا شد", Toast.LENGTH_SHORT).show();
        }
    };

    private View.OnClickListener onCancelbuttonClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            edtName.setText("");
            edtPhone.setText("");
            edtAddress.setText("");
            findViewById(R.id.lin_add_search).setVisibility(View.VISIBLE);
            findViewById(R.id.add_edit_employee).setVisibility(View.GONE);
        }
    };

    private View.OnClickListener onNewEmployeeSubmit = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (edtName.getText().length() == 0 || edtPhone.getText().length() == 0 || edtAddress.getText().length() == 0) {
                Toast.makeText(getActivity(), "لطفا اطلاعات را وارد کنید", Toast.LENGTH_SHORT).show();
                return;
            }
            Employee employee = new Employee();
            employee.setName(edtName.getText().toString());
            employee.setPhone(edtPhone.getText().toString());
            employee.setAddress(edtAddress.getText().toString());
            new DatabaseHelper(getActivity()).addNewEmployee(employee);
            refreshList();
            btnCancel.performClick();
        }
    };

    private View.OnClickListener onEditEmployeeSubmit = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (edtName.getText().length() == 0 || edtPhone.getText().length() == 0 || edtAddress.getText().length() == 0) {
                Toast.makeText(getActivity(), "لطفا اطلاعات را وارد کنید", Toast.LENGTH_SHORT).show();
                return;
            }
            Employee employee = new Employee();
            employee.setId(seletcedEmployeeId);
            employee.setName(edtName.getText().toString());
            employee.setPhone(edtPhone.getText().toString());
            employee.setAddress(edtAddress.getText().toString());
            new DatabaseHelper(getActivity()).editEmployee(employee);
            refreshList();
            btnCancel.performClick();
        }
    };
}

并获得了&#39; 10100&#39;

的值

但如果我确定x是数字,我会得到正确的值(110)

  var cost = (x * .1) + x;
  console.log(cost);

我总得到118.8。

答案 1 :(得分:1)

当您从输入控件中检索值时,它会以字符串值的形式出现。

var x = form["acertscore"].value; // x is a string

如果您立即将其转换为数字,则可以避免其他问题。

var x = Number(form["acertscore"].value); // x is a number

function costCalc() {
  var form = document.forms["operation_form"];
  var x = Number(form["acertscore"].value);
  var cost = (x * .1) + x;
  var answer = (cost * .08) + cost;
  form["answerbox"].value = answer;
}
<form name="operation_form">
  <label style="font-size:20px"><b>Price Tool</b></label>
  <br/>
  <br/>
  <br/>
  <label>Item Price:  </label><input type="number" name="acertscore" value="" />
  <br/>
  <br/>
  <label><b>Total is:</b></label><input type="number" name="answerbox">
  <br/>
  <input type="button" value="Calculate" onclick="costCalc();" />
  <br/>
  <input type="button" value="Reset" onClick="this.form.reset()" />
</form>

答案 2 :(得分:-1)

问题是您的answer号码太长,以至于它试图显示指数。因此,虽然您已指定输出进入<input> type number字段,但该数字会被视为字符串。由于您的answer不是数字,因此无法显示。

要解决此问题,您可以在显示之前将输出解析为float

form["answerbox"].value = parseFloat(answer);

&#13;
&#13;
function costCalc() {
  var form = document.forms["operation_form"];
  var x = form["acertscore"].value;
  var cost = (x * .1) + x;
  var answer = (cost * .08) + cost;
  form["answerbox"].value = parseFloat(answer);
}
&#13;
<form name="operation_form">
  <label style="font-size:20px"><b>Price Tool</b></label>
  <br/>
  <br/>
  <br/>
  <label>Item Price:  </label><input type="number" name='acertscore' value="" />
  <br/>
  <br/>
  <label><b>Total is:</b></label><input type="number" name="answerbox">
  <br/>
  <input type="button" value="Calculate" onclick="costCalc();" />
  <br/>
  <input type="button" value="Reset" onClick="this.form.reset()" />
</form>
&#13;
&#13;
&#13;

希望这有帮助!