C# CODE static void Main()
{
Double a = 12.345678;
Console.Write(Math.Round(a,2));
}
//(将输出 12.35)
四舍五入:
static void Main()
{
Double a = 12.345678;//是要四舍五入的数
Console.Write((a*10000+0.5)/10000);
}
//(将输出 12.345678)
double d = 2343.334;
decimal k =Convert.ToDecimal(34545.2323);
string j = d.ToString("f2");
string jkl = k.ToString("f2");
Response.Write(jkl);
JS
<SCRIPT LANGUAGE="JavaScript">
<!--
function formatFloat(src, pos)
{
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
alert(formatFloat("1212.2323", 2));
//-->
</SCRIPT>
还有如下几种方法:
var test=11111.111;
alert(test.toFixed(2));
// Float数据四舍五入到2位小数;
function to2bits(flt) {
if(parseFloat(flt) == flt)
return Math.round(flt * 100) / 100;
// 到4位小数, return Math.round(flt * 10000) / 10000;
else
return 0;
}
[转载文章]
asp保留两位小数
<%=Formatnumber(-888.888,3,-1,-1,0)%>
(888.900)
例子用到了函数Formatnumber()的所有参数:
第一个参数(-6665.8999)指定了要进行格式化的数字。
第二个参数(3)指定了小数点后显示的位数。
第三个参数(-1)指定了是否显示前导的零。
第四个参数(-1)指定是否对负数使用括号。
最后一个参数(0)指定是否显示分隔符
window.οnlοad=function ko()
{
var test=111.5552;
alert(Math.round(test));
alert(test.toFixed(2));
alert(to2bits(2222.34343));
}
function to2bits(flt) {
alert(parseFloat(flt));
alert(Math.round(flt * 100));
if(parseFloat(flt) == flt)
return Math.round(flt * 100) / 100;
// 到4位小数, return Math.round(flt * 10000) / 10000;
else
return 0;
}