Solved

format into a percentage amount instead of 66.66666666 ect to 66

Posted on 2013-11-19
8
228 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

630 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