search
HomeWeb Front-endJS TutorialConversion of various formats of js string ToString, Format_javascript skills

If we all calculate the correct format and then display it, it will obviously be a waste of code and efficiency. Today I saw many formats that ToString can solve. I will summarize them for everyone, hoping it can be convenient for everyone.
1. Convert money format, only int type, float type, double type
double d = 400;
d.ToString("C"); //¥400.00
2.10 decimal Numbers, only int type numbers
int i=400;
i.ToString("D5"); // 00400
3. Scientific numbers, only int type, float type, double type
float f = 400;
f.ToString("E");//4.000000E 002
4. Fixed format numbers, only int type, float type, double type
int i =400;
i.ToString("F3");//400.000 Fn represents n digits after the decimal point, F2 and F represent 2 digits after the decimal point
5.N numeric type
400000000000.ToString("N ")// 400,000,000,000.00" N will convert the number to oh digits after the decimal point, and there will be one every 3 digits.
The difference between it and C is that there is no preceding ¥ symbol
6.16 hexadecimal
400000000000. ToString("x")//"5d21dba000" Convert numbers to hexadecimal numbers
================== Date format conversion====== ==============
Date format In addition to the classes already encapsulated by Datetime, you can also use string .Format(); to convert to a specified format
string .Format("{0:f}",System.DateTime.Now);//Thursday, August 4, 2011 11:23
string.Format("{0:F}", System.DateTime.Now );//Thursday, August 4, 2011 11:23:53
dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
dt .GetDateTimeFormats('t')[0].ToString();//14:06
dt.GetDateTimeFormats('y')[0].ToString();//November 2005
dt. GetDateTimeFormats('D')[0].ToString();//November 5, 2005
dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
dt .GetDateTimeFormats('D')[2].ToString();//Saturday 2005 11 05
dt.GetDateTimeFormats('D')[3].ToString();//Saturday November 5, 2005
dt.GetDateTimeFormats('M')[0].ToString();//November 5
dt.GetDateTimeFormats('f')[0].ToString();//November 5, 2005 Day 14:06
dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
dt.GetDateTimeFormats('r')[0].ToString( );//Sat, 05 Nov 2005 14:06:25 GMT

string.Format("{0:d}",dt);//2005-11-5
string.Format( "{0:D}",dt);//November 5, 2005
string.Format("{0:f}",dt);//November 5, 2005 14:23
string.Format("{0:F}",dt);//November 5, 2005 14:23:23
string.Format("{0:g}",dt);//2005 -11-5 14:23
string.Format("{0:G}",dt);//2005-11-5 14:23:23
string.Format("{0:M} ",dt);//November 5
string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
string.Format(" {0:s}",dt);//2005-11-05T14:23:23
string.Format("{0:t}",dt);//14:23
string.Format ("{0:T}",dt);//14:23:23
string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
string.Format("{0:U}",dt);//November 5, 2005 6:23:23
string.Format("{0:Y}",dt);// November 2005
string.Format("{0}",dt);//2005-11-5 14:23:23

string.Format("{0:yyyyMMddHHmmssffff}", System.DateTime.Now);
yyyy represents the year MM represents the month dd represents the day HH represents the hour mm represents the minute ss represents the second ffff represents the second with 4 decimal places

Write so much for now, if I will continue to modify it if I find anything in the future.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP格式化一个 GMT/UTC 日期/时间PHP格式化一个 GMT/UTC 日期/时间Mar 21, 2024 am 10:41 AM

这篇文章将为大家详细讲解有关PHP格式化一个GMT/UTC日期/时间,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。格式化PHP中的GMT/UTC日期/时间简介在php中,格式化GMT/UTC日期/时间对于正确显示和处理跨时区日期至关重要。本文将介绍如何使用PHP的DateTime类格式化GMT/UTC日期/时间,以及各种可用的格式化选项。DateTime类DateTime类表示一个日期和时间。它可以存储和操作GMT/UTC等时区中的日期/时间值。要创建新的Da

使用StringBuffer类的toString()方法将StringBuffer转换为字符串使用StringBuffer类的toString()方法将StringBuffer转换为字符串Jul 25, 2023 pm 06:45 PM

使用StringBuffer类的toString()方法将StringBuffer转换为字符串在Java中,StringBuffer类是用于处理可变字符串的类,它提供了许多方便的方法来修改和操作字符串。当我们需要将一个StringBuffer对象转换为字符串时,可以使用toString()方法来实现。StringBuffer类的toString()方法返回一

python中的format是什么意思python中的format是什么意思Jul 31, 2023 pm 02:02 PM

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。

format在python中的用法是什么format在python中的用法是什么Jul 31, 2023 pm 01:59 PM

format在python中的用法是基本用法、指定位置、指定变量名、格式化数字、格式化日期和时间。

format在python中的含义是什么format在python中的含义是什么Jul 31, 2023 pm 02:05 PM

在Python中,`format`是一个内置函数,用于对字符串进行格式化处理。它用于创建带有占位符的字符串模板,并将指定的值填充到占位符中。这样可以根据不同的情形动态地构建字符串,使输出更具可读性和可定制性。

MySQL中如何将字符串转成format格式的日期时间MySQL中如何将字符串转成format格式的日期时间Jun 01, 2023 pm 09:22 PM

STR_TO_DATE(date,format):将字符串转成format格式的日期时间SELECTSTR_TO_DATE(‘2015-01-01',‘%Y-%m-%d')->2015-01-01

Python中的字符串格式化方式:format()函数的使用方法Python中的字符串格式化方式:format()函数的使用方法Apr 22, 2023 pm 07:01 PM

变量插入字符串的方法Python中的format()函数是一种将变量插入字符串的方法,能够使字符串更易于阅读和理解。它支持许多不同的用法,以下是具体的用法和说明:使用位置参数传递变量name='John'age=25print('Mynameis{},andIam{}yearsold.'.format(name,age))#输出:MynameisJohn,andIam25yearsold.使用索引传递变量name='

Java 中如何自定义实现 toString() 方法Java 中如何自定义实现 toString() 方法Apr 27, 2023 pm 02:25 PM

模拟实现tostring函数publicstaticStringmyToString(int[]array){Stringstr="[";for(inti=0;i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.