I have the weight grabbed from an xml as 0.800, I want to convert it to 000008. This Eg. 003456 is 345.6 kilograms shows how it is to be represented for heavier weights, could someone give me a heads up as to where I could start with this one?
thanks!
EDIT: I have the solution but does anyone know why it cuts off the last digit? See below:
XML:
<GROSSWEIGHT>345.800</GROSSWEIGHT>
PHP:
$LINE_WEIGHT = ($shipment->COLLO->GROSSWEIGHT*10); // 6 CHARS
sprintf("%06s", $LINE_WEIGHT)
// output 003450
EDIT: If i follow this, it works:
echo str_pad(0.800*10, 6, "0", STR_PAD_LEFT);
// output 000008
AS SOON as I include xml data:
echo $shipment->COLLO->GROSSWEIGHT; // 345.800
echo str_pad($shipment->COLLO->GROSSWEIGHT*10, 6, "0", STR_PAD_LEFT);
// output 003450
Please see pic below:
use *10 and then "pad-left":
echo str_pad(0.800*10, 6, "0", STR_PAD_LEFT); //000008
echo str_pad(345.6*10, 6, "0", STR_PAD_LEFT); //003456
You'd first need to multiply by 10, then have a look at sprintf, especially the padding option.
I'll answer my own question to how I decided to go ahead coding. I used the sprintf solution suggested by simon as it seems a lot cleaner.
XML:
GROSSWEIGHT>0.800</GROSSWEIGHT>
PHP:
$LINE_WEIGHT = (utf8_decode($shipment->COLLO->GROSSWEIGHT)*10); // 6 CHARS
sprintf("%06d", $LINE_WEIGHT);
Thanks everyone!
Related
I have a page with this global on it:
$sampleIssue = array('vol'=>25,'no'=>3 and 4);
on another page, I have this code which returns a 1 -???? What is wrong? I am sure it is something simple, but i am new enough to php not to know. I have searched white spaces in variables and strings, but still can't find the answer.
echo $sampleIssue['no'];
Change
$sampleIssue = array('vol'=>25,'no'=>3 and 4);
to
$sampleIssue = array('vol'=>25,'no'=>'3 and 4');
"3 and 4" is a string. You have to put it with in quotes. 25 will not show any problem because it is a number
$sampleIssue = array('vol'=>25,'no'=>3 and 4);
You need some quotes around 3 and 4, like
$sampleIssue = array('vol'=>25,'no'=>'3 and 4');
The expression 3 and 4 without quotes evaluates to Are both the number 3 and the number 4 "true"?, which itself is true. If you echo out a PHP boolean true, it displays as "1".
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
My number is 495 plain and simple.
I'm trying to format this number to have it display $4.95
what I've done is number_format($number, 2, '.', '')
but that outputs 495.00
I know it can't be this difficult. Anyone familiar with number_format?
I just need it to add a decimal two numbers from the right so the cents are always displayed. ex. 4.95, 12.58, 23.39... You get the idea.
Thank you.
If you're working with integers instead of floats, you'd first have to divide by 100.
$number = 495;
echo number_format($number/100, 2, '.', '')
Assuming your numbers are all whole numbers you can do the following:
$mynum = 495;
function formatMyNum($num) {
return $num/100;
}
Number format just displays floats using N number of decimals and the decimal/thousand separator characters you choose.
If you passed 4.95 to it, it would show fine so to do that just divide your $number by 100 before passing it to the function.
echo number_format($number/100, 2, '.', '');
I'm trying to add thousand separators to a number using PHP and at the same time keep the leading zeros (It's part of the design of an app that the leading 0s stay so that people can see the number grow towards the set target - a 6 figure number).
My initial attempt was to use str_pad to add the leading zeros if the current number calculated was less than 6 figures long. Then to add the commas I used number_format. The obvious issue is that number_format removes the leading 0s.
$num = 550;
$num_padded = str_pad($num, 6, '0', STR_PAD_LEFT);
echo number_format($num_padded);
So that returns 550 instead of 000,550
Does anyone know of a reliable way to achieve the format I'm looking for?
Thanks!
I have in mind this simple trick:
function padAndFormat($number, $length)
{
if(strlen($number)>=$length)
{
return number_format($number);
}
$number = number_format('1'.str_pad($number, $length-1, '0', STR_PAD_LEFT));
$number[0] = '0';
return $number;
}
//var_dump(padAndFormat('517', 6)); //string(7) "000,517"
another way to do this is to use sprintf
// length can be changed, here is 6
implode(',',str_split(sprintf('%06d', $this->iterator),3));
the result will be :
input
result
4
000,004
400
000,400
23560
023,560
1234567
1,234,567
it can be improved by reading the length, and computing automatically the final length, multiple of 3
I don't need number format but I need that comma! :)
<?php
$zeroes=0;
$num = 550;
$num_padded = str_pad($zeroes, 3, '0', STR_PAD_LEFT);
$num_padded = str_pad($num_padded,strlen($num_padded)+strlen($zeroes), ',', STR_PAD_RIGHT);
$num_padded = str_pad($num_padded,strlen($num_padded)+strlen($num), $num, STR_PAD_RIGHT);
echo $num_padded;
OUTPUT :
000,550
One Liner with PHP string manipulation. Works for any number of digits:
function pad($number, $min_digits){
return strrev(implode(",",str_split(str_pad(strrev($number), $min_digits, "0", STR_PAD_RIGHT),3)));
}
/* Output for 9 digits
0,000,001
0,000,012
0,000,123
0,001,234
0,012,345
0,123,456
1,234,567
12,345,678
123,456,789
/**/
I'am doing a call to a database to retrieve an ID of a specific item. Usually there are the ids like: 1, 2, 14, 23, .... I need to get the ID from a DB and output it 4 digit number.
For exemple:
My result ID is: 1. Now i when i have this value, i want it to become 0001. Or if my result id is 13, it should became 0013, etc.
How can i achieve that with php without changing the real ID in database?
You want to zerofill an integer.
You could either do sprintf('%04u', $n) or str_pad($n, 4, '0', STR_PAD_LEFT).
Same as always.
sprintf("%04d", $num)
You want the PHP strpad function:
<?php
$input = 1;
echo str_pad($input, 4, "0", STR_PAD_LEFT); // produces "0001"
?>
If you want to do it inside of MySQL query, here it is
SELECT LPAD(id, 4, '0') as modified_id FROM table;