当前位置:网站首页>String formatting
String formatting
2022-07-07 10:24:00 【Still work hard!】
In the process of programming , You need to convert numeric variables into string variables in a certain format , Here are some basic format conversion statements :
Format currency
string.Format("{0:C}",0.2); //¥0.20( Under English operating system :$0.20)
By default, this method formats two decimal places after the decimal point , If you need to keep one or more , You can specify the number of digits , Use the rounding method when intercepting .
string.Format("{0:C1}",0.25); //¥0.2
string.Format("{0:C1}",0.26); //¥0.3
Number formatting
- Format fixed digit decimal digits
string.Format("{0:D3}",23); //023
string.Format("{0:D3}",2343); //2343
This method can format numbers into fixed digits , And it only supports plastic surgery . The specified fixed number of digits indicates the minimum number of digits required in the result string , When the specified fixed number of digits is less than the number of digits of the number itself , The result string is the number itself .
- Use commas to divide numbers and specify the number of digits after the decimal point
string.Format("{0:N}",230869.55); //230869.55
string.Format("{0:N1}",230869.55); //230869.5

By default, this method formats two decimal places after the decimal point , If you need to keep one or more , You can specify the number of digits , Use the rounding method when intercepting .
- Format as percentage
string.Format("{0:P}",0.23256); //23.26%
string.Format("{0:P1}",0.23255); //23.3%
By default, this method formats two decimal places after the decimal point , If you need to keep one or more , You can specify the number of digits , Use the rounding method when intercepting .
Place holder
- Zero placeholder
string.Format("{0:0000.00}",23243.214); //023
string.Format("{0:0000.00}",243.215); //2343
This can be done by “ Place on digit 0” To format numbers , If the target number has a number on the corresponding digit, the number will be retained , If there is no number, use 0 fill . amount to “ Fixed digit decimal digits ” The enhanced . rounding .
2. Digital placeholders
string.Format("{0:####.##}",23243.214); //023
string.Format("{0:####.##}",243.215); //2343
The method and “ Zero placeholder ” The effect is similar to , Use “#” To identify the form of numbers , If the target number has a number on the corresponding digit, the number will be retained , If there is no number, omit the digit .
3. Space placeholder
string A = string.Format("{0,-25}", " Align left , Not enough space ");
A += "( end )";
Date formatting
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 year 6 month 11 Japan , Saturday
string.Format("{0:f}", DateTime.Now); // 2022 year 6 month 11 Japan , Saturday 15:58
string.Format("{0:F}", DateTime.Now); // 2022 year 6 month 11 Japan , Saturday 15:58:16
string.Format("{0:m}", DateTime.Now); // 6 month 11 Japan
string.Format("{0:t}", DateTime.Now); // 15:58
string.Format("{0:T}", DateTime.Now); // 15:58:16
The above string formatting statement can convert non string variables into strings in a certain format , The conversion effect cannot be achieved for variables that are strings .
边栏推荐
- 嵌入式背景知识-芯片
- Some test points about coupon test
- Study summary of postgraduate entrance examination in October
- Postman interface test II
- BigDecimal数值比较
- ISP、IAP、ICP、JTAG、SWD的编程特点
- Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
- Study summary of postgraduate entrance examination in August
- HDU-2196 树形DP学习笔记
- Why is the reflection efficiency low?
猜你喜欢

ArcGIS operation: batch modify attribute table

High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors

The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
![[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet](/img/3c/890f2481231482645554f86dd614a9.png)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet

The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.

0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)

Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记

Postman interface test I

STM32基础知识—内存映射

1321:【例6.3】删数问题(Noip1994)
随机推荐
搭建物联网硬件通信技术几种方案
Guid主键
UnityWebRequest基础使用之下载文本、图片、AB包
浅谈日志中的返回格式封装格式处理,异常处理
Vs code specifies the extension installation location
The physical meaning of imaginary number J
Embedded background - chip
Use the fetch statement to obtain the repetition of the last row of cursor data
2022.7.3DAY595
IPv4 socket address structure
STM32 ADC and DMA
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Appx代碼簽名指南
Socket通信原理和实践
[second on] [jeecgboot] modify paging parameters
Experience sharing of software designers preparing for exams
ES6中的函數進階學習
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Postman interface test VII
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?