有小数量,要修剪到小数点后2位(如果存在) [英] Have decimal amount, want to trim to 2 decimal places if present

查看:99
本文介绍了有小数量,要修剪到小数点后2位(如果存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有小数量,要修剪到小数点后2位(如果存在)

Have decimal amount, want to trim to 2 decimal places if present

推荐答案

您是否尝试过使用值= Decimal.Round(价值2)

例如:

using System;

class Test
{    
    static void Main()
    {
        decimal d = 1234.5678m;
        Console.WriteLine("Before: {0}", d); // Prints 1234.5678
        d = decimal.Round(d, 2);
        Console.WriteLine("After: {0}", d); // Prints 1234.57
    }
}

请注意,这是舍入,而不是刚刚修剪(所以这里的四舍五入)......你到底需要什么?该小数结构支持任何你需要做的机会。更多的选项,请查阅 MSDN

Note that this is rounding rather than just trimming (so here it's rounded up)... what exactly do you need? Chances that the Decimal struct supports whatever you need to do. Consult MSDN for more options.

这篇关于有小数量,要修剪到小数点后2位(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆