Detailed explanation of formatting numbers in MySQL

Detailed explanation of formatting numbers in MySQL

Recently, due to work needs, I need to format numbers in MySQL, but I found that there is little information on the Internet. I simply summarize it myself to help friends in need. Let's take a look at the detailed introduction together:

1. format function:

Format floating point number format(number, length);

Description: Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part. D should be a constant value.

Sample Code

mysql> SELECT FORMAT(12332.123456, 4);
 -> '12,332.1235' 
mysql> SELECT FORMAT(12332.1,4);
 -> '12,332.1000' 
mysql> SELECT FORMAT(12332.2,0);
 -> '12,332'

2. rpad and lpad give a number of digits, but if it is insufficient, add custom characters

RPAD:

Returns the string str, right-padded with the string padstr to a length of len characters. If
str is longer than len, the return value is shortened to len characters.

Sample Code

mysql> SELECT RPAD('hi',5,'?'); 
  -> 'hi???'
 mysql> SELECT RPAD('hi',1,'?'); 
  -> 'h'
mysql>SELET RPAD(12, 5 ,0);
  ->12000

This function is multi-byte safe.

LPAD:

Returns the string str, left-padded with the string padstr to a length of lencharacters. If str is longer than len, the return value is shortened to lencharacters.

Sample Code

mysql> SELECT LPAD('hi',4,'??');
 -> '??hi' 
mysql> SELECT LPAD('hi',1,'??');
 -> 'h' 

mysql>SELECT LPAD(12, 5, 0)
  ->'00012'

Reference: http://www.cnblogs.com/fenglie/articles/4409208.html

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • MySQL format decimals retain two decimal places (decimal format)
  • Detailed explanation of date formatting in MySQL
  • Detailed explanation of mysql to get the current date and format
  • A brief introduction to MySQL database formatting
  • Detailed explanation of mysql get current date function and time formatting parameters

<<:  Application of Hadoop counters and data cleaning

>>:  Detailed explanation of the use and differences of various lock mechanisms in Linux

Recommend

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

10 excellent Web UI libraries/frameworks

1. IT Mill Toolkit IT Mill Toolkit is an open sou...

Implementation methods of common CSS3 animations

1. What is CSS Animations is a proposed module fo...

Vue backend management system implementation of paging function example

This article mainly introduces the implementation...

Nginx signal control

Introduction to Nginx Nginx is a high-performance...

How to use MySQL covering index and table return

Two major categories of indexes Storage engine us...

Linux remote login implementation tutorial analysis

Linux is generally used as a server, and the serv...

Nginx load balancing algorithm and failover analysis

Overview Nginx load balancing provides upstream s...

How to set up a shared folder on a vmware16 virtual machine

1. Set up a shared folder on the virtual machine:...

Detailed explanation of MySql slow query analysis and opening slow query log

I have also been researching MySQL performance op...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

JavaScript BOM Explained

Table of contents 1. BOM Introduction 1. JavaScri...

Tutorial diagram of installing centos7.3 on vmware virtual machine

VMware Preparation CentOS preparation, here is Ce...

Detailed explanation of redo log and undo log in MySQL

The most important logs in the MySQL log system a...