Solved

format into a percentage amount instead of 66.66666666 ect to 66

Posted on 2013-11-19
8
224 Views
Last Modified: 2013-11-23
trying to set up a value formatted to cut all off on the right side of the decimal point

<td width="15%" align="right">
                        <span id="perecnt_<?php echo ($i);?>" class="tablchet" ><?php echo sprintf($currmask,0);?></span>
                  </td>


perecntpur.innerHTML = ((1/amtstore) *amtbought) * 100 ; to display


format
0
Comment
Question by:sydneyguy
8 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 150 total points
ID: 39659269
Referenced from
http://php.net/manual/en/function.sprintf.php

<?php
$number = 362525200;

echo sprintf("%.0e", $number); // outputs 3.625e+8
?>

This should give you the percentage format you need :)
0
 

Author Comment

by:sydneyguy
ID: 39659277
this is the line that does the maths so its this line that will need the formatting applied to it

perecntpur.innerHTML = ((1/amtstore) *amtbought) * 100 ; to display
0
 

Author Comment

by:sydneyguy
ID: 39659281
am trying this line but does not work

perecntpur.innerHTML =  sprintf("%.0e",((1/amtstore) *amtbought) * 100);
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 150 total points
ID: 39659295
In PHP you have the round function, will will round a float to an int. If you just want to trim of the decimals then you have floor(). innerHTML is Javascript so I'm confused as to which language you're referring to:

$number = 66.66666666;

echo round($number); // will echo 67
echo floor($number); // will echo 66

Open in new window

No idea why you're trying to format your number in scientific notation
0
 

Author Comment

by:sydneyguy
ID: 39659328
this syntax i would have guessed would have worked but no
perecntpur.innerHTML = round(((1/amtstore) *amtbought) * 100);

val = round(((1/amtstore) *amtbought) * 100);
perecntpur.innerHTML =  val;

any ideas but we are gettgn there
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 39659333
Here is how I would do it in PHP, assuming that you have the data in PHP and you're not constrained to use only client-side data.

http://www.laprbass.com/RAY_temp_sydneyguy.php

Outputs:
0.666666666667
0.67
67%
66.7%

<?php // RAY_temp_sydneyguy.php
error_reporting(E_ALL);
echo '<pre>';


// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297972.html
// FORMAT A PERCENTAGE VALUE


// CREATE A REPEATING DECIMAL
$x = 2.0 / 3.0;
echo $x;
echo PHP_EOL;

// TURN IT INTO A USEFUL DECIMAL VALUE
$y = number_format($x, 2);
echo $y;
echo PHP_EOL;

// TURN IT INTO A PERCENTAGE
$z = number_format($x * 100, 0) . '%';
echo $z;echo PHP_EOL;

// TURN IT INTO A PERCENTAGE
$z = number_format($x * 100, 1) . '%';
echo $z;echo PHP_EOL;

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39659470
round() is a PHP function.

This line of your code...

perecntpur.innerHTML = round(((1/amtstore) *amtbought) * 100);

Open in new window

...does not look like PHP. What is it?
0
 

Author Closing Comment

by:sydneyguy
ID: 39670961
thanks for the help on this question
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Restricting the scope of variables to private in OOP 4 22
PHP breaks when used in Wordpress template file 3 29
jQuery Toggle & Anchor Links 5 43
Wordpress Pagination 1 28
These days socially coordinated efforts have turned into a critical requirement for enterprises.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question