如何用C锐利舍入十进制数?

时间:2019-12-24 16:56:01

标签: c#

我正在尝试编写一些将英镑转换为美元的代码。 转换工具工作正常。但是,我正在努力四舍五入十进制数字。 例如,当我转换£24.50时,我的工具要输出$ 31.75时,我的工具将输出$ 31.8500。 我已经尝试过Math.Round();方法,但不幸的是,我并未失败。

这是我的代码,我在做什么错了?

using System;
using System.Windows.Forms;

namespace currencyconverter
{
public partial class currencyconv : Form
{


    decimal US_Dollars = Math.Round(1.30m,2);
    decimal Australian_Dollars = 1.87m;
    decimal European_Euros = 1.17m;



    public currencyconv() 
    {
        InitializeComponent();
    }

    private void currencyconv_Load(object sender, EventArgs e)
    {

        cmbcurrency.Text = "Select a currency";

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void btnConvert_Click(object sender, EventArgs e)
    {
        if (cmbcurrency.SelectedIndex == -1 || (string.IsNullOrWhiteSpace(txtconvert.Text)))
        {
            System.Windows.Forms.MessageBox.Show("Please complete the required fields.");

        }

        else
            {
            decimal British_Pound = decimal.Parse(txtconvert.Text);
            if (cmbcurrency.Text == "USD")
            {
                txtresult.Text = System.Convert.ToString(("$" + British_Pound * US_Dollars));
            }

            if (cmbcurrency.Text == "AUD")
            {

                txtresult.Text = System.Convert.ToString(("$" + British_Pound * Australian_Dollars));
            }

            if (cmbcurrency.Text == "EUR")
            {

                txtresult.Text = System.Convert.ToString(("€" + British_Pound * European_Euros));
            }
        }
    }

    private void txtconvert_KeyPress(object sender, KeyPressEventArgs e)
    {
        Char chr = e.KeyChar;
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
        {
            e.Handled = true;
            MessageBox.Show("Please enter a numerical value.");
        }

        if(e.KeyChar == '.' && (txtconvert.Text.IndexOf('.') > -1)){

            e.Handled = true;
        }

    }
}
}

2 个答案:

答案 0 :(得分:1)

我尝试了以下操作,以得到特定的31.75美元。您需要使用1.296作为将英镑转换为美元的汇率。

    decimal US_Dollars = 1.296m;

    Console.WriteLine("$" + string.Format("{0:0.00}", (decimal)24.50 * US_Dollars));
    // output: $31.75

在您的代码中,您将使用以下内容

    if (cmbcurrency.Text == "USD")
    {
        txtresult.Text = System.Convert.ToString("$" + string.Format("{0:0.00}", British_Pound * US_Dollars));
    }

建议:使用网络api提取费率,而不是硬编码要转换的数字。 This is one example:

此代码是使用API​​获得正确的转化率

    public static double GetRate(string baseFormat, string resultFormat)
    {
        RestClient client = new RestClient($"https://api.exchangeratesapi.io/latest?base={baseFormat}"); // CHange the base to whichever you are converting
        RestRequest request = new RestRequest(Method.GET);
        request.AddHeader("Accept", "*/*");
        var response = client.Execute(request);

        var rates = JObject.Parse(response.Content)["rates"];
        return double.Parse(rates[resultFormat].ToString());
    }

    //Usage
    double US_Dollars = GetRate("GBP", "USD");
    Console.WriteLine("$" + string.Format("{0:0.00}", (double)24.50 * US_Dollars));


// output: $31.74

答案 1 :(得分:0)

您需要做的是获取转换后的数字并使用String.Format,如下所示:

String.Format("{0:0.00}", 123.4567); 

第一个参数是您想要的格式,第二个参数是您想要转换为给定格式的值。

我想这就是您所需要的。

相关问题