转:C# 小数位数保留的方法集锦】的更多相关文章

转载自:http://www.jb51.net/article/17010.htm 1. System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); provider.NumberDecimalDigits =intDecLength; //要设定的小数位数 double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text)…
1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();provider.NumberDecimalDigits =intDecLength; //要设定的小数位数double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內的值转成doublethis.txtCashAmt.Text = str…
package com.qiyuan.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalUtils { /** * (1)按四舍五入保留指定小数位数,位数不够用0补充(一般不这么用) * @param o:格式化前的小数 * @param newScale:保留小数位数 * @return 格式化后的小数 */…
1.使用占位符: 1)float f = 321.12345F;f.ToString("0.00");这样做无论f是不是整数,都将加上2位小数. 2)float f = 321.12345F;f.ToString(".##");这样做最多显示2位小数 占位符的定义如下: (0) Digit placeholder. Display a digit or a zero. If the expression has a digit in the position whe…
decimal保留指定位数小数的时候,.NET自带的方法都是四舍五入的. 项目中遇到分摊金额的情况,最后一条的金额=总金额-已经分摊金额的和. 这样可能导致最后一条分摊的时候是负数,所以自己写了一个保留指定位数小数的方法. 扩展方法的使用,使得调用起来很优雅. public static class DecimalExtension { /// <summary> /// decimal保留指定位数小数 /// </summary> /// <param name="…
方法一: ); 方法二: Math.Round() 方法三: double dbdata = 0.55555; string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入 方法四: string result = String.Format("{0:N2}", 0.55555);//2位 string result = String.Format("{0:N3}", 0.55555);//3位 方法五:…
1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); provider.NumberDecimalDigits =intDecLength; //要設定的小數位數 double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內的值轉成double this.txtCashAmt.Text =…
  在C#中大家都会遇到这种情况 double类型的数据,需要格式化(保留N未有效数字)或者是保留N为小数等情况,我们往往采取double.tostring("参数");的方法.下面就列出几个常用的方法. double temp=3.1415926; (F)Fixed point:string str1=temp.toString("f1");//保留一位小数 四舍五入 结果:3.1 (F)Fixed point:string str2=temp.toString(…
php保留小数格式,定义小数格式,小数点,位数,小数位数: 方法一:(推荐)bcmul(1000.90,1,2);//两个数相乘1000.90*1, 保留两位小数点(无四舍五入)<返回string类型>方法二:(无四舍五入)numberFormat(1000.999);function numberFormat($a=0){$d = strpos($a, ".");if($d){$tmp = explode(".", $a);$a = $tmp[0].&…
package hello; import java.util.Arrays; public class 实验三更正版 { public static void main(String[] args) { // TODO Auto-generated method stub // TODO Auto-generated method stub double a[]={15,12,18,0,6,99,8}; double s=0; for(int i=0;i<a.length;i++) { Sys…