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);// ...
随机推荐
- 近期对招聘Android开发者的一些思考
公司要招聘Android开发者,故面试了大概十来个人.由于是小公司,所以来的人大多是90后,比較年轻.90后大概二十三四岁吧,从简历上看都写了一到两年的工作经验. 也由于是小公司,所以对工作经验这些没 ...
- STM8在IAR中Option Byte配置
转http://www.stmcu.org/module/forum/thread-607140-1-1.html 近期帮客户调试STM8的程序使用到OptionByte配置,在网上查询几乎都是介绍如 ...
- 【转载】使用 Google Guava 美化你的 Java 代码
转载地址: https://blog.csdn.net/wisgood/article/details/13297535 原文地址:https://my.oschina.net/leejun2005/ ...
- [转]SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- ELK日志相关
转: Logstash 讲解与实战应用 原创qw871122016-08-20 16:06:07评论(1)40217人阅读 一.Logstash 介绍 Logstash 是一款强大的数据处理工具,它可 ...
- 基于tornado的爬虫并发问题
tornado中的coroutine是python中真正意义上的协程,与python3中的asyncio几乎是完全一样的,而且两者之间的future是可以相互转换的,tornado中有与asyncio ...
- kafaka学习
创建一个topic: [root@hdp1 bin]# ./kafka-topics. --replication-factor --partitions --topic justin Created ...
- kafka 监控(eagle)
topic:创建时topic名称 partition:分区编号 offset:表示该parition已经消费了多少条message logSize:表示该partition已经写了多少条message ...
- kibana 的search 的的搜索提示挡住输入框
解决方案就是 [root@10-19-148-59 styles]# pwd /data/package/kibana/src/public/styles [root@10-19-148-59 sty ...
- schema的作用
1,如果schema中定义的字段类型和数据库中该字段存储值的类型不一致(可以不定义,但定义的时候类型必须一致),则该字段查找不到,mongoose不会返回该字段的数据 2,如果数据库中有字段a,而sc ...