Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to format a string to a fixed length 1

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
0
0
US
I have a quantity field that I need toend up with in a field with a length of 14 places. the field is of a format 14.4 so I tried using

Code:
$vQty = sprintf("%014.4f", $data[7]);

The error I got was because I am not allowed to have a decimal point in the result.

How can I get the resuly 00000000010000 for a quantity of 1 rather than 0000000001.0000?
 
If the field is already in the format of 14.4, you could use the int function to only return the integer portion of the floating point number.

If you just need to remove the last 4 digits, you could use the substr function or a regex to return all but the last 4 digits.

There are other ways, but those should give you a place to start.
 
The only way I see is
Code:
$vQty = sprintf("%014d", $data[7]*10000);
where 'd' is for unsigned numbers. You should also check that your numbers are not too large for the multiplication.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top