String function Library: sprintf

Source: Internet
Author: User
Keywords PHP Chinese function Manual sprintf
Tags aliyun example function function library html http php php chinese function manual

sprintf

(PHP3, PHP4)

sprintf---&http://www.aliyun.com/zixun/aggregation/37954.html ">nbsp; Returns a formatted string

Syntax: string sprintf (string format [, mixed args ...])

Description:

Returns a string, according to the format of the formatted string.

The format string is made up of 0 (zero) or some commands, and normal characters (not including%) are copied directly to the result, which applies to sprintf () and printf ().

The specifications for each conversion are composed of these elements, in the following sequence:

The non-essential option specifies padding, indicating what characters are used to fill the result into the appropriate string length, which can be a blank character or 0 (character 0), which is preset to be padded with blanks. To replace a padding character, you can specify it by preceding the first word with a single quotation mark ('), referring to the following example.
Non-required options specify alignment to indicate whether the result should be left-aligned or right-aligned, the preset is right-aligned, and character-can be used to align it to the left.

The Not-required option specifies the width, indicating how many (or least) characters the transformation will produce.

Non-required options specify precision, which indicates that to display to the decimal point, this option has no effect on the type other than floating-point numbers, and another function Number_format () helps to format the number.

Refers to the shape of the state to describe the parameters of the form, the possible types are:

%-percent character, no arguments required

B-treats the argument as an integer, and the result is a binary value

C-treats the argument as an integer and the result is a character of this ASCII value

D-treats the argument as an integer, and the result is a value of 10 decimal

F-treats the parameter as a floating-point number, and the result is a floating-point number

O-Treats the argument as an integer and the result is an octal value

S-treats both parameters and results as strings

X-treats the argument as an integer and results in a value of 16 (lowercase letters)

X-treats the argument as an integer and results in a value of 16 (uppercase letters)

Example:

<?php

$isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day);

?>

Example:

<?php

$money 1 = 68.75;

$money 2 = 54.35;

$money = $money 1 + $money 2;

echo $money would output "123.1";

$formatted = sprintf ("%01.2f", $money);

echo $formatted would output "123.10"

?>

Reference: printf () sscanf () fscanf () Number_format ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.