输入字符串格式不正确,从十进制到字符串?

时间:2017-05-18 08:46:44

标签: c#

我正在尝试将小数转换为字符串,这是我的代码: 我已更新代码,我有教科书查看货币值,不允许在运行时编辑它只是为了显示其他货币数字的结果。

public void doRequest(final ApiCallback callback) {
    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

    Call<ActivitiesResponse> call = apiService.getUserActivities(id);
    call.enqueue(new Callback<ActivitiesResponse>() {
        // If success
        @Override
        public void onResponse(Call<ActivitiesResponse>call, Response<ActivitiesResponse> response) {

             list = response.body().getActivities();
             callback.onSuccess(list); // pass the list
        }
        // If failed
        @Override
        public void onFailure(Call<ActivitiesResponse>call, Throwable t) {
            // Log error here since request failed
            Log.e(TAG, t.toString());
        }
    });
}

public interface ApiCallback{
    void onSuccess(ArrayList<YOURTYPE> result);
}

我的代码图片

enter image description here

2 个答案:

答案 0 :(得分:0)

我一般建议拆分代码,即

//decimal dcj = decimal.Parse(sj);
decimal dcj;
if (!decimal.TryParse(sj, out decimal))
{
    throw new ApplicationException("...");
}

mtb_SAL_TAX02.Text = string.Format("{0:#,###,##0.00}",dcj);

另外,如果你的字符串表示十进制可能有误,我宁愿选择tryparse。

答案 1 :(得分:0)

试试吧。您应该在解析时设置文化。我认为您的计算机没有使用标准文化设置。

不要使用CurrentCulture,让我们更改为InvariantCulture或特定文化

var result = string.Format("{0:#,###,##0.00}", decimal.Parse("10,000.00", System.Globalization.CultureInfo.InvariantCulture));

或者你可以根据需要设置特定的文化

var result = string.Format("{0:#,###,##0.00}", decimal.Parse("10,000.00", new System.Globalization.CultureInfo("en-EN")));
相关问题