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
of the decimal) in the format expression, display leading or trailing
zeros. If the number has more digits to the right of the decimal
separator than there are zeros to the right of the decimal separator in
the format expression, round the number to as many decimal places as
there are zeros. If the number has more digits to the left of the
decimal separator than there are zeros to the left of the decimal
separator in the format expression, display the extra digits without
modification.

(#) 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
and trailing zeros aren't displayed if the number has the same or fewer
digits than there are # characters on either side of the decimal
separator in the format expression.

(.) 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
numeric placeholders. For example, you could define a format expression
as "The formatted number is: ###,###.#0"

在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”表示一个数字
(0-9)。如果该数字为负,则该字符串以减号开头。小数点前总有一个数字。精度说明符指示小数点后所需的位数。如果省略精度说明符,则使用默认值,即小
数点后六位数字。格式说明符的大小写指示在指数前加前缀“E”还是“e”。指数总是由正号或负号以及最少三位数字组成。如果需要,用零填充指数以满足最少
三位数字的要求。

F 或 f

固定点

数字转换为“-ddd.ddd…”形式的字符串,其中每个“d”表示一个数字 (0-9)。如果该数字为负,则该字符串以减号开头。精度说明符指示所需的小数位数。如果忽略精度说明符,则使用 NumberFormatInfo 给定的默认数值精度。

G 或 g

常规

根据数字类型以及是否存在精度说明符,数字会转换为固定点或科学记数法的最紧凑形式。如果精度说明符被省略或为零,则数字的类型决定默认精度,如下表所示。

  • Byte 或 SByte:3

  • Int16 或 UInt16:5

  • Int32 或 UInt32:10

  • Int64 或 UInt64:19

  • Single:7

  • Double:15

  • Decimal:29

如果用科学记数法表示数字时指数大于 -5
而且小于精度说明符,则使用固定点表示法;否则使用科学记数法。如果要求有小数点,并且忽略尾部零,则结果包含小数点。如果精度说明符存在,并且结果的有
效数字位数超过指定精度,则通过舍入删除多余的尾部数字。使用科学记数法时,如果格式说明符是“G”,结果的指数带前缀“E”;如果格式说明符是“g”,
结果的指数带前缀“e”。

上述规则有一个例外:如果数字是 Decimal 而且省略精度说明符时。在这种情况下总使用固定点表示法并保留尾部零。

N 或 n

数字

数字转换为“-d,ddd,ddd.ddd…”形式的字符串,其中每个“d”表示一个数字
(0-9)。如果该数字为负,则该字符串以减号开头。小数点左边每三个数字之间插入一个千位分隔符。精度说明符指示所需的小数位数。如果忽略精度说明符,
则使用 NumberFormatInfo 给定的默认数值精度。

P 或 p

百分比

数字转换为由 NumberFormatInfo.PercentNegativePattern 属性或
NumberFormatInfo.PercentPositivePattern 属性定义的、表示百分比的字符串。如果数字为负,则产生的字符串由
PercentNegativePattern 定义并以负号开头。已转换的数字乘以 100
以表示为百分比。精度说明符指示所需的小数位数。如果省略精度说明符,则使用 NumberFormatInfo 给定的默认数值精度。

R 或 r

往返过程

往返过程说明符保证转换为字符串的数值再次被分析为相同的数值。使用此说明符格式化数值时,首先使用常规格式对其进行测试:Double 使用
15 位精度,Single 使用 7
位精度。如果此值被成功地分析回相同的数值,则使用常规格式说明符对其进行格式化。但是,如果此值未被成功地分析为相同数值,则它这样格式
化:Double 使用 17 位精度,Single 使用 9
位精度。虽然精度说明符可以追加到往返过程格式说明符,但它将被忽略。使用此说明符时,往返过程优先于精度。此格式仅受浮点型支持。

X 或 x

十六进制数

数字转换为十六进制数字的字符串。格式说明符的大小写指示对大于 9
的十六进制数字使用大写字符还是小写字符。例如,使用“X”产生“ABCDEF”,使用“x”产生“abcdef”。精度说明符指示结果字符串中所需的最
少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。只有整型才支持此格式。

在msdn中搜索“标准数字格式字符串”可得到

3、使用System.Math.Round函数可以指定小数位数。

C#数字类型输出字符串时保留指定小数位数的方法的更多相关文章

  1. Java四舍五入时保留指定小数位数

    方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP).doubleValue(); ...

  2. Lua保留指定小数位数

    默认会四舍五入 比如:%0.2f 会四舍五入后,保留小数点后2位 print(string.format("%.1f",0.26)) ---会输出0.3,而不是0.2 Lua保留一 ...

  3. Java指定保留小数位数的方法

    package com.qiyuan.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.tex ...

  4. sql server 删除所有表和递归查询、数字类型转为字符串

    1.删除所有表 select 'drop table '+name+';' from sys.tables where name like 'DataSyncV1DelaySample%' or na ...

  5. Python3:数字类型和字符串类型的相互转换

    Python3:数字类型和字符串类型的相互转换 一.python中字符串转换成数字 方法1: 类中进行导入:import string str='555'num=string.atoi(str)num ...

  6. 解决like '%字符串%'时索引不被使用的方法

    解决like '%字符串%'时索引不被使用的方法   分步阅读 解决like '%字符串%'时索引不被使用的方法,如果like以通配符开头('%abc')时索引会失效会变成全表扫描的操作. 工具/原料 ...

  7. 20181121笔记(for,数字类型和字符串类型的内置方法)

    1.for循环 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. for循环字典时默认取出key: dic={'x':111,'y':222,'z:333'}​for k in dic:​ ...

  8. Day2_数字类型_字符串类型_列表类型_元组_字典_集合_字符编码_文件处理

    数字类型: 作用:年纪,等级,薪资,身份证号等: 10进制转为2进制,利用bin来执行. 10进制转为8进制,利用oct来执行. 10进制转为16进制,利用hex来执行. #整型age=10 prin ...

  9. day05数据类型,数字类型,字符串类型,字符串的操作方法,列表类型的操作方法,可变类型与不可变类型

    复习 ''' 流程控制 1.顺序结构.分支结构.循环结构 2.if分支结构 if 条件: 代码块 elif 条件: 代码块 else: 代码块 # 可以被if转换为False:0 | '' | Non ...

随机推荐

  1. 1115 Counting Nodes in a BST (30 分)

    1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...

  2. fiddler基础功能介绍

  3. SpringBoot学习记(一)第一个SpringBoot Web服务

    工具IDEA 一.构建项目 1.选择Spring Initializr 2.填写项目信息 3.勾选webService 4.勾选Thymeleaf 5.项目建立完成,启动类自动生成 二.写个Contr ...

  4. xshell连不上虚拟机linux的解决办法(用的默认NAT模式)

    1.找到Linux系统的ip地址 输入命令   ifconfig 2.打开本地网络连接 将VMnet1的ip地址设置为和虚拟机ip同一网段的ip 比如虚拟机Linux系统的ip为   192.168. ...

  5. 使用eclipse在linux下开发C/C++

    一直在Linux下开发,苦于没有IDE,一般都是自己编写Makefile,然后在windows下用文本编辑器ftp打开文件编辑,然后在linux下完成编译.调试代码也只能是命令行用gdb进行调试,相当 ...

  6. CentOS重新加载网卡报错 Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/

    重新加载网卡时出现的错误如下: 1 [root@vdb1 dev]# service network restart 2 Shutting down interface eth0: Device st ...

  7. 根据inode编号来删除文件或目录

    在Linux系统上,有时候会出现文件名为特殊字符的文件或目录,当我们使用rm来删除这样的文件或目录时,就会出错导致删不掉.但是我们可以依据inode号来删除这样的文件,方法如下: (1)执行ls -i ...

  8. display属性详解

    内容: 1.display介绍 2.display分类 3.块级标签和内联标签 4.inline-block应用 1.display介绍 display:display属性设置元素如何被显示 2.di ...

  9. 暴搜 - Codeforces Round #327 (Div. 2) E. Three States

    E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...

  10. Laravel基础

    一.Laravel核心目录文件介绍 app:程序的核心代码和业务逻辑代码,其中的Http目录是我们业务逻辑的存放点 bootstrap:包含框架启动的和自动加载文件 config:包含所有程序中的配置 ...