C#数字类型输出字符串时保留指定小数位数的方法
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 where the 0 appears in the format string, display it; otherwise, display a zero in that position. If the number has fewer digits than there are zeros (on either side |
| (#) | Digit placeholder.Display a digit or nothing. If the expression has a digit in the position where the # appears in the format string, display it; otherwise, display nothing in that position. This symbol works like the 0 digit placeholder, except that leading |
| (.) | Decimal placeholder. In some locales, a comma is used as the decimal separator. The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator. If the format expression contains only number signs to the left of this symbol, numbers smaller than 1 begin with a decimal separator. To display a leading zero displayed with fractional numbers, use 0 as the first digit placeholder to the left of the decimal separator. The actual character used as a decimal placeholder in the formatted output depends on the Number Format recognized by your system. |
| (,) | Thousand separator. In some locales, a period is used as a thousand separator. The thousand separator separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. Standard use of the thousand separator is specified if the format contains a thousand separator surrounded by digit placeholders (0 or #). The actual character used as the thousand separator in the formatted output depends on the Number Format recognized by your system. |
| ("ABC") |
Literal string. You can place literal strings on either side of |
在msdn中搜索FormatString property可得到
2、使用标准数字格式字符串
如:
float f = 321.12345F;
f.ToString("F3");
结果为321.123
标准数字格式字符串的定义如下:
| 格式说明符 | 名称 | 说明 |
|---|---|---|
|
C 或 c |
货币 |
数字转换为表示货币金额的字符串。转换由用于格式化数字的 NumberFormatInfo 对象的货币格式信息控制。精度说明符指示所需的小数位数。如果省略精度说明符,则使用 NumberFormatInfo 给定的默认货币精度。 |
|
D 或 d |
十进制数 |
只有整型才支持此格式。数字转换为十进制数字 (0-9) 的字符串,如果数字为负,则前面加负号。精度说明符指示结果字符串中所需的最少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。 |
|
E 或 e |
科学计数法(指数) |
数字转换为“-d.ddd…E+ddd”或“-d.ddd…e+ddd”形式的字符串,其中每个“d”表示一个数字 |
|
F 或 f |
固定点 |
数字转换为“-ddd.ddd…”形式的字符串,其中每个“d”表示一个数字 (0-9)。如果该数字为负,则该字符串以减号开头。精度说明符指示所需的小数位数。如果忽略精度说明符,则使用 NumberFormatInfo 给定的默认数值精度。 |
|
G 或 g |
常规 |
根据数字类型以及是否存在精度说明符,数字会转换为固定点或科学记数法的最紧凑形式。如果精度说明符被省略或为零,则数字的类型决定默认精度,如下表所示。
如果用科学记数法表示数字时指数大于 -5 上述规则有一个例外:如果数字是 Decimal 而且省略精度说明符时。在这种情况下总使用固定点表示法并保留尾部零。 |
|
N 或 n |
数字 |
数字转换为“-d,ddd,ddd.ddd…”形式的字符串,其中每个“d”表示一个数字 |
|
P 或 p |
百分比 |
数字转换为由 NumberFormatInfo.PercentNegativePattern 属性或 |
|
R 或 r |
往返过程 |
往返过程说明符保证转换为字符串的数值再次被分析为相同的数值。使用此说明符格式化数值时,首先使用常规格式对其进行测试:Double 使用 |
|
X 或 x |
十六进制数 |
数字转换为十六进制数字的字符串。格式说明符的大小写指示对大于 9 |
在msdn中搜索“标准数字格式字符串”可得到
3、使用System.Math.Round函数可以指定小数位数。
C#数字类型输出字符串时保留指定小数位数的方法的更多相关文章
- Java四舍五入时保留指定小数位数
方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP).doubleValue(); ...
- Lua保留指定小数位数
默认会四舍五入 比如:%0.2f 会四舍五入后,保留小数点后2位 print(string.format("%.1f",0.26)) ---会输出0.3,而不是0.2 Lua保留一 ...
- Java指定保留小数位数的方法
package com.qiyuan.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.tex ...
- sql server 删除所有表和递归查询、数字类型转为字符串
1.删除所有表 select 'drop table '+name+';' from sys.tables where name like 'DataSyncV1DelaySample%' or na ...
- Python3:数字类型和字符串类型的相互转换
Python3:数字类型和字符串类型的相互转换 一.python中字符串转换成数字 方法1: 类中进行导入:import string str='555'num=string.atoi(str)num ...
- 解决like '%字符串%'时索引不被使用的方法
解决like '%字符串%'时索引不被使用的方法 分步阅读 解决like '%字符串%'时索引不被使用的方法,如果like以通配符开头('%abc')时索引会失效会变成全表扫描的操作. 工具/原料 ...
- 20181121笔记(for,数字类型和字符串类型的内置方法)
1.for循环 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. for循环字典时默认取出key: dic={'x':111,'y':222,'z:333'}for k in dic: ...
- Day2_数字类型_字符串类型_列表类型_元组_字典_集合_字符编码_文件处理
数字类型: 作用:年纪,等级,薪资,身份证号等: 10进制转为2进制,利用bin来执行. 10进制转为8进制,利用oct来执行. 10进制转为16进制,利用hex来执行. #整型age=10 prin ...
- day05数据类型,数字类型,字符串类型,字符串的操作方法,列表类型的操作方法,可变类型与不可变类型
复习 ''' 流程控制 1.顺序结构.分支结构.循环结构 2.if分支结构 if 条件: 代码块 elif 条件: 代码块 else: 代码块 # 可以被if转换为False:0 | '' | Non ...
随机推荐
- 利用python,简单的词语纠错
利用python,编写一个简单的词语纠正修改器. 原文:http://norvig.com/spell-correct.html #!/usr/bin/env python # coding=utf- ...
- AWS CLI以及AWS S3 SYNC命令行使用
1.到AWS的IAM创建用户,并且获取到访问密钥 ID 和私有访问密钥.下载密钥并保存. 2.到http://docs.amazonaws.cn/cli/latest/userguide/instal ...
- springMVC 是单例还是的多例的?
曾经面试的时候有面试官问我spring的controller是单例还是多例,结果我傻逼的回答当然是多例,要不然controller类中的非静态变量如何保证是线程安全的,这样想起似乎是对的,但是不知道( ...
- Delphi Webbrowser使用方法详解(一)
1.webbroser介绍 该组件是一个浏览器组件,可以显示一个指定地址的网页.设置网页打开时的主页以及对网页进行相关的操作,同时也可以对HTML文件进行剪切.复制.粘贴.删除等操作.该 组件在Int ...
- class装载原理
原理图: 1.执行引擎是Java虚拟机实现的核心,用于处理各种指令. 2.PC寄存器用于存储线程下一次指令的地址和返回值地址,虚拟机为每个线程创建单独的PC寄存器.如果执行的是本地方法,PC寄存器的值 ...
- python中__init__()、__new__()、__call__()、__del__()几个魔法方法的用法
关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...
- IPv4检验和计算
IP分组中的检验和仅覆盖首部,而不管数据,首部被划分为16位的段,把所有段相加,结果取反,塞进首部检验和里 在目的主机中,首部划分为16位,相加,结果肯定是16个1,然后取反,结果为0.如下 在目的主 ...
- YUM软件包额外扩展了解项
5.YUM配置文件 yum的配置一般有两种方式: 一种是直接配置/etc目录下的yum.conf文件, 另外一种是在/etc/yum.repos.d目录下增加.repo文件 [root@xuliang ...
- xe7 Unresolved external CSPIN.OBJ
工程里打开使用了CSPIN控件的界面窗体,再编译就好了 [ilink32 Error] Error: Unresolved external 'Vcl::Controls::TControl::Set ...
- J2SE 8的输入输出--读取/写入文本文件和读取/写入二进制数据
读取/写入文本文件 // 1. 文本输入 // (1) 短小文本直接转入字符串 String string = new String(Files.readAllBytes(Paths.get(&quo ...