当前位置:网站首页>字符串格式化
字符串格式化
2022-07-07 08:06:00 【还是要努力呀!】
在程序编写过程中,需要将数值型变量按照一定的格式转化为字符串变量,下面介绍一些基本格式转换的语句:
格式化货币
string.Format("{0:C}",0.2); //¥0.20(英文操作系统下:$0.20)
该方法默认格式化小数点后面保留两位小数,如需保留一位或是更多,可以指定位数,截取时采用五舍六入法。
string.Format("{0:C1}",0.25); //¥0.2
string.Format("{0:C1}",0.26); //¥0.3
数字格式化
- 格式化固定位数的十进制数字
string.Format("{0:D3}",23); //023
string.Format("{0:D3}",2343); //2343
该方法可以将数字格式化为固定的位数,并且只支持整形。所指定的固定位数表明结果字符串中所需最少数字个数,当所指定的固定位数小于该数字本身的位数时,结果字符串为该数字本身。
- 使用逗号将数字分割并指定小数点后的位数
string.Format("{0:N}",230869.55); //230869.55
string.Format("{0:N1}",230869.55); //230869.5
该方法默认格式化小数点后面保留两位小数,如需保留一位或是更多,可以指定位数,截取时采用五舍六入法。
- 格式化为百分比
string.Format("{0:P}",0.23256); //23.26%
string.Format("{0:P1}",0.23255); //23.3%
该方法默认格式化小数点后面保留两位小数,如需保留一位或是更多,可以指定位数,截取时采用四舍五入法。
占位符
- 零占位符
string.Format("{0:0000.00}",23243.214); //023
string.Format("{0:0000.00}",243.215); //2343
该方法可以通过“在数位上置0”来设置数字的格式,目标数字在相应的数位上有数字时则保留该数字,无数字时则使用0填充。相当于“固定位数的十进制数字”的加强版。四舍五入。
2. 数字占位符
string.Format("{0:####.##}",23243.214); //023
string.Format("{0:####.##}",243.215); //2343
该方法与“零占位符”的效果类似,使用“#”来标识数字的形式,目标数字在相应的数位上有数字时则保留该数字,无数字时则省略该数位。
3. 空格占位符
string A = string.Format("{0,-25}", "左对齐,不足补空格");
A += "(结束)";
日期格式化
string.Format("{0:d}", DateTime.Now); // 2022/6/11
string.Format("{0:g}", DateTime.Now); // 2022/6/11 15:58
string.Format("{0:G}", DateTime.Now); // 2022/6/11 15:58:16
string.Format("{0:D}", DateTime.Now); // 2022年6月11日,星期六
string.Format("{0:f}", DateTime.Now); // 2022年6月11日,星期六 15:58
string.Format("{0:F}", DateTime.Now); // 2022年6月11日,星期六 15:58:16
string.Format("{0:m}", DateTime.Now); // 6月11日
string.Format("{0:t}", DateTime.Now); // 15:58
string.Format("{0:T}", DateTime.Now); // 15:58:16
以上字符串格式化语句可以将非字符串的变量按照一定格式转化为字符串,对于本身就是字符串的变量来说无法取得转换效果。
边栏推荐
- 2022.7.3DAY595
- Or in SQL, what scenarios will lead to full table scanning
- ArcGIS operation: batch modify attribute table
- ORM模型--关联字段,抽象模型类
- ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
- The physical meaning of imaginary number J
- .NET配置系统
- ORM模型--数据记录的创建操作,查询操作
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- Agile course training
猜你喜欢
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
ES6中的原型对象
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
The physical meaning of imaginary number J
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
对存储过程进行加密和解密(SQL 2008/SQL 2012)
SQLyog数据库怎么取消自动保存更改
【剑指Offer】42. 栈的压入、弹出序列
随机推荐
对存储过程进行加密和解密(SQL 2008/SQL 2012)
Postman interface test III
Smart city construction based on GIS 3D visualization technology
Why is the reflection efficiency low?
Win10 installation vs2015
Google Colab装载Google Drive(Google Colab中使用Google Drive)
Appx code signing Guide
Easyexcel read write simple to use
IPv4 socket address structure
Deadlock caused by non clustered index in SQL Server
ORM模型--数据记录的创建操作,查询操作
ORM--查询类型,关联查询
request对象对请求体,请求头参数的解析
搭建物联网硬件通信技术几种方案
学习记录——高精度加法和乘法
Es classes and objects, prototypes
[sword finger offer] 42 Stack push in and pop-up sequence
Interface test
[ORM framework]
Appx代码签名指南