Solved

format into a percentage amount instead of 66.66666666 ect to 66

Posted on 2013-11-19
8
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

735 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