Home Web Front-end JS Tutorial Conversion of various formats of js string ToString, Format_javascript skills

Conversion of various formats of js string ToString, Format_javascript skills

May 16, 2016 pm 06:03 PM
format tostring

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 of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP formats a GMT/UTC date/time PHP formats a GMT/UTC date/time Mar 21, 2024 am 10:41 AM

This article will explain in detail how to format a GMT/UTC date/time with PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Introduction to Formatting GMT/UTC Date/Time in PHP In PHP, formatting GMT/UTC date/time is crucial for correctly displaying and handling cross-time zone dates. This article will explain how to format a GMT/UTC date/time using PHP's DateTime class, as well as the various formatting options available. DateTime class The DateTime class represents a date and time. It can store and manipulate date/time values ​​in time zones such as GMT/UTC. To create a new Da

Convert StringBuffer to string using toString() method of StringBuffer class Convert StringBuffer to string using toString() method of StringBuffer class Jul 25, 2023 pm 06:45 PM

Convert a StringBuffer to a string using the toString() method of the StringBuffer class. In Java, the StringBuffer class is a class used to handle mutable strings. It provides many convenient methods to modify and manipulate strings. When we need to convert a StringBuffer object into a string, we can use the toString() method to achieve this. The toString() method of the StringBuffer class returns a

What does format in python mean? What does format in python mean? Jul 31, 2023 pm 02:02 PM

format in Python is a string formatting method used to insert variables or values ​​into placeholder positions in a string. Through the format method, we can dynamically construct a string to contain different values.

What is the usage of format in python What is the usage of format in python Jul 31, 2023 pm 01:59 PM

The usage of format in python is basic usage, specifying location, specifying variable name, formatting numbers, formatting date and time.

What does format mean in python What does format mean in python Jul 31, 2023 pm 02:05 PM

In Python, `format` is a built-in function used to format strings. It is used to create a string template with placeholders and fill the placeholders with specified values. This allows strings to be constructed dynamically based on different situations, making the output more readable and customizable.

How to convert a string into formatted date and time in MySQL How to convert a string into formatted date and time in MySQL Jun 01, 2023 pm 09:22 PM

STR_TO_DATE(date, format): Convert the string into the date and time in format SELECTSTR_TO_DATE(‘2015-01-01’,‘%Y-%m-%d’)->2015-01-01

String formatting in Python: How to use the format() function String formatting in Python: How to use the format() function Apr 22, 2023 pm 07:01 PM

Method of inserting variables into a string The format() function in Python is a method of inserting variables into a string, which can make the string easier to read and understand. It supports many different usages. The following are specific usages and instructions: Use positional parameters to pass variables name='John'age=25print('Mynameis{},andIam{}yearsold.'.format(name,age))#output :MynameisJohn,andIam25yearsold. Use the index to pass the variable name='

How to customize the toString() method in Java How to customize the toString() method in Java Apr 27, 2023 pm 02:25 PM

Simulate the implementation of the tostring function publicstaticStringmyToString(int[]array){Stringstr="[";for(inti=0;i

See all articles