How to change the displayed number of digits [duplicate] - php

This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
Hi all just a very quick one.
How would I write in an sql query or php to change the number of digits displayed.
For example I am using this $values['ClientId'] which is a AI primary key, I know that until I get to 10 it will look like 1, 2, 3, 4,...,10, but I want it to look like 01, 02, 03. or even 001.
Probably a real simple one but I c
ant find it.

Use the str_pad function:
http://php.net/manual/en/function.str-pad.php
str_pad( $number, $padLength, $padWith, STR_PAD_LEFT );
str_pad( "1", 4, "0", STR_PAD_LEFT ); // gives 0001

$input=1; //if you need 01 instated of 1 then try
echo sprintf("%02d", $input);
$input=1; //if you need 001 instated of 1 then try
echo sprintf("%03d", $input);
please read this sprintf

Related

convert a number to decimal number without rounding [duplicate]

This question already has answers here:
How to make number_format() not to round numbers up
(18 answers)
Closed 3 years ago.
I have this number
$sku = '2200081005966';
and I want to convert the number like this without rounding the number
$new_sku = '5.96'
I already try this number_format(substr($sku, 7, 12), 0, '', '.')
but the output that I get 5.97.
Any ideas how can I make this work?
Thank you.
Add floor() around your number_format:
$sku = '2200081005966';
echo floor(number_format(substr($sku, 7, 12), 0, '', '.')*100)/100;
Outputs:
5.96
Note: It would works well in case of positive numbers, your substr always take a positive number, that's why it would be enough.
<?php
$sku = '2200081005966';
$foo=substr($sku, 9, 12);
echo substr($foo, 0,2).".".substr($foo, 3,3);
?>
first you get the last 4 charakters of the string, then you split that part and put a dot between.

Print number with spaces as thousands place separator [duplicate]

This question already has answers here:
PHP - Correct way to add space between numbers?
(3 answers)
Closed 1 year ago.
I'm trying to reverse a string and add spaces from 3 in 3 characters the code I have right now is this:
$rowPreco = "925000"
$rowPreco = strrev($rowPreco);
$rowPreco = wordwrap($rowPreco , 3 , ' ' , true );
$rowPreco = strrev($rowPreco);
if I take the strrev out it prints how I want ("925 000") but if I have the strrev it will print ("92 500 0").
But I need to use the strrev because if the value is ("1200000") it will print ("120 000 0") instead of ("1 200 000").
Any help would be appreciated.
Thanks!
UPDATE!
When I use the number_format it will ignore a jQuery code that I have.
echo "<script>"
."jQuery(document).ready(function() {"
."var parts = jQuery('.casaPreco').text().split('|');"
."jQuery('.casaPreco2').text(parts[4]);"
."});"
."</script>";
Basically the initial string is something like this: "3880562|1|1|925000|||0|0|0"
I need to grab the 4th number so I'm splitting the string on the "|" and use the array parts[4] but when I use the number_format it will grab the number "3880562"
Please do not invent the wheel:
echo number_format('925000', 0, '.', ' '); // 925 000
echo number_format('1200000', 0, '.', ' '); // 1 200 000
number_format manual.

PHP number format 4 digits [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 9 years ago.
For example, I have this format:
$s_number = "12"; And for this input, I need 0012 as the output:
Another example:
Input $s_number = "3";
Output: 0003
Every time I need 4 digits I would like this to happen.
It won't be a number (but a string), but you can do that using str_pad. In your examples:
$s_number = str_pad( "12", 4, "0", STR_PAD_LEFT );
$s_number = str_pad( "3", 4, "0", STR_PAD_LEFT );
Use str_pad() for that:
echo str_pad($number, 4, '0', STR_PAD_LEFT);
You can achieve this easily by using phps' printf
<?php
$s_number = '12';
printf("%04d", $s_number);
?>
Hope this helps

php function to convert numbers into currency format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP number_format is rounding?
I need a PHP function to convert numbers into currency format like so:
444 output 444.00
444.156 output 444.15
0 output 0.00
It should not round off the last decimal digit.
try number_format method. It will get the job done for you.
$number = 444.657;
$format_number = number_format($number, 2, '.', '');
// 444.66
Without Rounding
substr(number_format($number, 3, '.', ''), 0, -1);
//444.65

Minus value using php [duplicate]

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have the value as $title =10 and I want to minus it with 01.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09 but this code not like this it still have the result $minus=9. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round() returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);

Categories