Solved

format into a percentage amount instead of 66.66666666 ect to 66

Posted on 2013-11-19
8
220 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
 
LVL 42

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 108

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 42

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now