String Format for Double [C#]
转载:http://www.csharp-examples.net/string-format-double/
he following examples show how to format float numbers to string in C#. You can use static method String.Format or instance methods double.ToString and float.ToString.
Digits after decimal point
This example formats double to string with fixed number of decimal places. For two decimal places use pattern „0.00“. If a float number has less decimal places, the rest digits on the right will be zeroes. If it has more decimal places, the number will be rounded.
[C#]
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
Next example formats double to string with floating number of decimal places. E.g. for maximal two decimal places use pattern „0.##“.
[C#]
// max. two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
Digits before decimal point
If you want a float number to have any minimal number of digits before decimal point use N-times zero before decimal point. E.g. pattern „00.0“ formats a float number to string with at least two digits before decimal point and one digit after that.
[C#]
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567); // "123.5"
String.Format("{0:00.0}", 23.4567); // "23.5"
String.Format("{0:00.0}", 3.4567); // "03.5"
String.Format("{0:00.0}", -3.4567); // "-03.5"
Thousands separator
To format double to string with use of thousands separator use zero and comma separator before an usual float formatting pattern, e.g. pattern „0,0.0“ formats the number to use thousands separators and to have one decimal place.
[C#]
String.Format("{0:0,0.0}", 12345.67); // "12,345.7"
String.Format("{0:0,0}", 12345.67); // "12,346"
Zero
Float numbers between zero and one can be formatted in two ways, with or without leading zero before decimal point. To format number without a leading zero use # before point. For example „#.0“ formats number to have one decimal place and zero to N digits before decimal point (e.g. „.5“ or „123.5“).
Following code shows how can be formatted a zero (of double type).
[C#]
String.Format("{0:0.0}", 0.0); // "0.0"
String.Format("{0:0.#}", 0.0); // "0"
String.Format("{0:#.0}", 0.0); // ".0"
String.Format("{0:#.#}", 0.0); // ""
Align numbers with spaces
To align float number to the right use comma „,“ option before the colon. Type comma followed by a number of spaces, e.g. „0,10:0.0“ (this can be used only in String.Format method, not in double.ToString method). To align numbers to the left use negative number of spaces.
[C#]
String.Format("{0,10:0.0}", 123.4567); // " 123.5"
String.Format("{0,-10:0.0}", 123.4567); // "123.5 "
String.Format("{0,10:0.0}", -123.4567); // " -123.5"
String.Format("{0,-10:0.0}", -123.4567); // "-123.5 "
Custom formatting for negative numbers and zero
If you need to use custom format for negative float numbers or zero, use semicolon separator „;“ to split pattern to three sections. The first section formats positive numbers, the second section formats negative numbers and the third section formats zero. If you omit the last section, zero will be formatted using the first section.
[C#]
String.Format("{0:0.00;minus 0.00;zero}", 123.4567); // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero"
Some funny examples
As you could notice in the previous example, you can put any text into formatting pattern, e.g. before an usual pattern „my text 0.0“. You can even put any text between the zeroes, e.g. „0aaa.bbb0“.
[C#]
String.Format("{0:my number is 0.0}", 12.3); // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3); // "12aaa.bbb3"
String Format for Double [C#]的更多相关文章
- string.Format 处理 double 的问题
出处: http://www.cnblogs.com/albert-struggle/archive/2012/05/22/2512744.html 1.格式化货币(跟系统的环境有关,中文系统默认格式 ...
- 学习java时在要求输出的数字带俩个小数点时,利用String.format时出现的问题
public class StringFormatDemo { public static void main(String[] args) { //String.format 实现了四舍五入 Sys ...
- 【转】string.Format对C#字符串格式化
转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...
- String.Format 格式说明
C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...
- C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}等等
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); / ...
- 如何在string.Format()方法中输出大括号
在string.Format参数中,大括号{}是有特殊意义的符号,但是如果我们希望最终的结果中包含大括号({}),那么我们需要怎么做呢?是”\{”吗?很遗憾,运行时,会给你一个Exception的!正 ...
- String.Format格式说明——复制于DotNet笔记
String.Format格式说明 C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 st ...
- C# String.Format格式说明
C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...
- C#:String.Format数字格式化输出
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...
随机推荐
- gcc cc1: all warnings being treated as errors
cc1: all warnings being treated as errors 在Makefile中找到 -Werror项,删除即可.删除后重新编译. 或设置环境变量 c工程设置 export C ...
- 【转载整理】Hibernater的锁机制
转载原文:http://www.cnblogs.com/otomedaybreak/archive/2012/01/27/2330008.html 概要:数据库事务,事务并发,hibernate悲观锁 ...
- nginx location 配置详解 【转载,整理】
http://www.nginx.cn/115.html NGINX location 配置参考:http://www.cnblogs.com/zlingh/p/6288994.html https: ...
- [转]Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
以前解析json用的惯的就是Google的gson了,用惯了基本就用它了,一直也没发现什么大问题,因为都是解析简单的json数据.但是最近学习springboot,要解析一个比较复杂的json数据.就 ...
- unity, 立即生效动画:Animation.sample()
在调用了动画播放之后,动画并不会立即应用(骨骼Transform并不会立即改变),最快也要等到本帧lateUpdate才能生效. 如果有特殊需求,希望在调用了动画播放之后立即生效,则可以紧接着调一句A ...
- java FileReader/FileWriter读写文件
java FileReader/FileWriter读写字母和数字没问题,但读写汉字就乱码.记录下,后面找到解决方法再补上. public static void main(String[] args ...
- ITOO高校云平台V3.1--项目总结(二)
自身责任要明白 心态要明白 布置任务要有反馈 总结 今天下午.举办了一场ITOO高校云平台3.1总结大会,针对3.1开发的过程中统计上来的问题进行讨论. 通过讨论统计上来的问题,映射到自身,看看自己还 ...
- linux分区详解
点评:如果你想了解linux分区,请务必认真阅读本文.对于初学者来说,linux分区不像windows下那么一目了解. Linux 分区的规定 1. 设备管理 在 Linux 中,每一 ...
- 关于iReport-5.6.0之前版本 “无法启动”或者“一开就关闭”的 某些原因
在经过Google搜索后.外国的大神说: We don't support Java 8 right now. Support will be added in next versions. 大概意思 ...
- ios Coredata 关联 UITableView 数据自动更新
昨天写了一篇关于coredata的文章,自己觉得挺傻的文章.没想其它程序员看过后觉得更傻,于是今天决定写一篇厉害点的,首先写了一个coredata和uitableview结合的框架,非常简单实现了数据 ...