猿问
下载APP

请问求一php自定义函数 用来规范价格?

求一php自定义函数 用来规范价格


ibeautiful
浏览 99回答 3
3回答

摇曳的蔷薇

很简单$money=22;$formatted = sprintf("%01.2f", $money);echo $formatted; //输出20.00下面给你这个函数的说明及用法:(phpManual上的,是英文的)上一页 CLXI. String 字符串处理函数&nbsp;下一页--------------------------------------------------------------------------------sprintf(PHP&nbsp;4,&nbsp;PHP&nbsp;5)sprintf — Return a formatted string说明string sprintf ( string $format [, mixed $args [, mixed $...]] )Returns a string produced according to the formatting string format.参数formatThe format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf() and printf().Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order:An optional sign specifier that forces a sign (- or +) to be used on a&nbsp;number. By default, only the - sign is used on a&nbsp;number if it's negative. This specifier forces positive&nbsp;numbers to have the + sign attached as well, and was added in PHP 4.3.0.An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). See the examples below.An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified.An optional number, a width specifier that says how many characters (minimum) this conversion should result in.An optional precision specifier that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.A type specifier that says what type the argument data should be treated as. Possible types:% - a literal percent character. No argument is required.b - the argument is treated as an integer, and presented as a binary number.c - the argument is treated as an integer, and presented as the character with that&nbsp;ASCII&nbsp;value.d - the argument is treated as an integer, and presented as a (signed) decimal number.e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).u - the argument is treated as an integer, and presented as an unsigned decimal number.f - the argument is treated as a float, and presented as a floating-point number (locale aware).F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3.o - the argument is treated as an integer, and presented as an octal number.s - the argument is treated as and presented as a string.x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).The format string supports argument numbering/swapping. Here is an example:例 2326. Argument swapping<?php$format = 'There are %d monkeys in the %s';printf($format, $num, $location);?>This might output, "There are 5 monkeys in the tree". But imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as:例 2327. Argument swapping<?php$format = 'The %s contains %d monkeys';printf($format, $num, $location);?>We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead:例 2328. Argument swapping<?php$format = 'The %2$s contains %1$d monkeys';printf($format, $num, $location);?>An added benefit here is that you can repeat the placeholders without adding more arguments in the code. For example:例 2329. Argument swapping<?php$format = 'The %2$s contains %1$d monkeys.That\'s a nice %2$s full of %1$d monkeys.';printf($format, $num, $location);?>args...返回值Returns a string produced according to the formatting string format.更新日志版本 说明4.0.6 Support for argument numbering/swapping was added范例例 2330. printf(): various examples<?php$n = 43951789;$u = -43951789;$c = 65; //&nbsp;ASCII&nbsp;65 is 'A'// notice the double %%, this prints a literal '%' characterprintf("%%b = '%b'\n", $n); // binary representationprintf("%%c = '%c'\n", $c); // print the&nbsp;ascii&nbsp;character, same as chr() functionprintf("%%d = '%d'\n", $n); // standard integer representationprintf("%%e = '%e'\n", $n); // scientific notationprintf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integerprintf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integerprintf("%%f = '%f'\n", $n); // floating point representationprintf("%%o = '%o'\n", $n); // octal representationprintf("%%s = '%s'\n", $n); // string representationprintf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integerprintf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer?>上例将输出:%b = '10100111101010011010101101'%c = 'A'%d = '43951789'%e = '4.39518e+7'%u = '43951789'%u = '4251015507'%f = '43951789.000000'%o = '247523255'%s = '43951789'%x = '29ea6ad'%X = '29EA6AD'%+d = '+43951789'%+d = '-43951789'例 2331. printf(): string specifiers<?php$s = 'monkey';$t = 'many monkeys';printf("[%s]\n", $s); // standard string outputprintf("[%10s]\n", $s); // right-justification with spacesprintf("[%-10s]\n", $s); // left-justification with spacesprintf("[%010s]\n", $s); // zero-padding works on strings tooprintf("[%'#10s]\n", $s); // use the custom padding character '#'printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters?>上例将输出:[monkey][ monkey][monkey ][0000monkey][####monkey][many monke]例 2332. sprintf(): zero-padded integers<?php$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);?>例 2333. sprintf(): formatting currency<?php$money1 = 68.75;$money2 = 54.35;$money = $money1 + $money2;// echo $money will output "123.1";$formatted = sprintf("%01.2f", $money);// echo $formatted will output "123.10"?>例 2334. sprintf(): scientific notation<?php$number = 362525200;echo sprintf("%.3e", $number); // outputs 3.625e+8?>参见printf()sscanf()fscanf()vsprintf()number_format()--------------------------------------------------------------------------------上一页 上一级 下一页soundex 起始页&nbsp;sscanf

幕布斯5086720

你要输出一个字符串:function type_str(){print("123");}这样的话,可以,但是如果你要判断是否输出成功了,你就必须这么写:function type_str(){if(print("123")){return true;}else return false;}你可以通过程序的返回值来判断是否输出成功。还有一些自定义函数处理后的结果也是需要返回的,就得用到return。比如:function add($a , $b){$sum = $a+$b;return $sum;}函数运行结束后,函数内部定义的变量全部会被销毁,所以,为了你处理的结果不会白白浪费掉,一定要将$sum 返回。再告诉你一个小窍门:echo和print是不同的,echo不是一个函数,更没有返回值,所以你不能写if(echo "123") ,但是 print是一个函数,它有返回值,成功则返回true,失败则返回false,可以写:if(print("123")); 之类的。所以print要比echo速度慢一点点,一般情况下用echo就行了。

收到一只叮咚

PHP函数分为系统内部函数 和 自定义函数。自定义函数 使用 Function 来自定义一个函数:格式:Function func_name($val){……}函数的命名跟自定义变量一样,只能使用 _, A~Z, a~z一个自定义函数中的变量是局部的,函数外不生效使用global全局变量的时候,在函数外才可使用变量(不建议)为了放置全局变量的混乱可以使用 unset($var)删除一个变量实例:<?phpfunction _pr(){echo "这是个自定义函数";}_pr();?>其输出结果:这是个自定义函数的赋值-------
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答