社区
C#
帖子详情
怎样定义小数位数?
xiaoslong
2004-11-20 12:12:50
比如我定义一个变量 double a; 然后赋植 a=53.5262;怎样才能指定变量a的小数位数为2,当用
Console.WriteLine(a)显示时,能自动转换为53.26。
...全文
474
4
打赏
收藏
怎样定义小数位数?
比如我定义一个变量 double a; 然后赋植 a=53.5262;怎样才能指定变量a的小数位数为2,当用 Console.WriteLine(a)显示时,能自动转换为53.26。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
parol2910
2004-11-20
打赏
举报
回复
double a=23.5678;
Response.Write(a.ToString("F2"));
输出为23.57
elusion
2004-11-20
打赏
举报
回复
toString("F2");
gully
2004-11-20
打赏
举报
回复
还有一种方法
string strInt = string.Format("{0:.00}",i); //显示两位小数,当小数部分为0时显示 .00
string strInt = string.Format("{0:.##}",i); //当小数部分为0时 不显示0.
占位符0 当输入为0时仍然显示0
占位符# 当输入为0时不显示
hxhbluestar
2004-11-20
打赏
举报
回复
//这是普通的字符串转换,小数位数在MyDouble.ToString("F")的F后面加数字就可以了,默认是两位的
using System;
using System.Threading;
using System.Globalization;
class Class1
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
double MyDouble = 123456789;
Console.WriteLine("The examples in en-US culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine("The examples in de-DE culture.\n");
Console.WriteLine(MyDouble.ToString("C"));
Console.WriteLine(MyDouble.ToString("E"));
Console.WriteLine(MyDouble.ToString("P"));
Console.WriteLine(MyDouble.ToString("N"));
Console.WriteLine(MyDouble.ToString("F"));
}
}
上述代码示例将下列内容显示到控制台。
The examples in en-US culture:
$123,456,789.00
1.234568E+008
12,345,678,900.00%
123,456,789.00
123456789.00
The examples in de-DE culture:
123.456.789,00 DM
1,234568E+008
12,345,678,900.00%
123.456.789,00
123456789,00
//这是用于要在货币值中使用的小数位数。InvariantInfo 的默认值为 2。
using System;
using System.Globalization;
class NumberFormatInfoSample {
public static void Main() {
// Gets a NumberFormatInfo associated with the en-US culture.
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
// Displays a negative value with the default number of decimal digits (2).
Int64 myInt = -1234;
Console.WriteLine( myInt.ToString( "C", nfi ) );
// Displays the same value with four decimal digits.
nfi.CurrencyDecimalDigits = 4;
Console.WriteLine( myInt.ToString( "C", nfi ) );
}
}
/*
This code produces the following output.
($1,234.00)
($1,234.0000)
*/
TIA博途中如何处理浮点
数
从而得到精确的小
数
点位
数
的具体方法.docx
TIA博途中如何处理浮点
数
从而得到精确的小
数
点位
数
的具体方法
输入一个浮点
数
,判断小
数
有几位——C语言代码
课程的随堂作业,C语言的,用dev就能运行,萌新代码,勿喷,仅仅帮助不想写作业的朋友方便一下,反正老师也不会仔细检查的
Android EditText限制输入整
数
和小
数
的位
数
的方法示例
直接上代码 新建DecimalInputTextWatcher类继承TextWatcher (代码可直接复制使用) import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; import android.widget.EditText; /** * EditText 限制输入整
数
和小
数
的位
数
* 默认 整
数
位无限制,
小
数
位
最多2位 */ public class DecimalInputTextWatcher implements TextWa
强制保留小
数
点后几位
可以强制的保留小
数
点后几位,达到精确的显示
自动保留
小
数
位
处理浮点
数
时,希望显示的
小
数
位
数
根据
数
字大小自动调整,比如大于1的
数
字,保留两位小
数
就可以了,但小于0.01的
数
字,希望能够自动为了精度保留四位小
数
,即0.0001。
C#
111,120
社区成员
642,538
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章