So I am trying to add a decimal point in front of a whole in (10) The issue is I'm trying to use sprintf(); but it seems to not be working for me :(.
Code:
sprintf(".",($percentamount));
Let me know what I am doing wrong thanks!
I have already tried "%.f"
if i understand you correctly you will take $percentamount variable which have to contain 10 as its value and just put '.' in front of it to make string '0.1' out of it - is it right? If yes than you could take 10 as digit and place it within a string as follows:
sprintf('.%d', 10); // %d stands for digit and means digit should be your second parameter
if you want to have 10 as string input you could write it as follows:
sprintf('.%s', '10');
if it is not the solution you looking for please describe it better and i'll try to help
Related
I need to parse a json that contains a long number (that was produces in a java servlet). The problem is the long number gets rounded.
When this code is executed:
var s = '{"x":6855337641038665531}';
var obj = JSON.parse(s);
alert (obj.x);
the output is:
6855337641038666000
see an example here: http://jsfiddle.net/huqUh/
why is that, and how can I solve it?
As others have stated, this is because the number is too big. However, you can work around this limitation by sending the number as a string like so:
var s = '{"x":"6855337641038665531"}';
Then instead of using JSON.parse(), you can use a library such as javascript-bignum to work with the number.
It's too big of a number. JavaScript uses double-precision floats for numbers, and they have about 15 digits of precision (in base 10). The highest integer that JavaScript can reliably save is something like 251.
The solution is to use reasonable numbers. There is no real way to handle such large numbers.
The largest number JavaScript can handle without loss of precision is 9007199254740992.
I faced this issue some time ago, I was able to solve using this lib: https://github.com/josdejong/lossless-json
You can check this example:
let text = '{"normal":2.3,"long":123456789012345678901,"big":2.3e+500}';
// JSON.parse will lose some digits and a whole number:
console.log(JSON.stringify(JSON.parse(text)));
// '{"normal":2.3,"long":123456789012345680000,"big":null}' WHOOPS!!!
// LosslessJSON.parse will preserve big numbers:
console.log(LosslessJSON.stringify(LosslessJSON.parse(text)));
// '{"normal":2.3,"long":123456789012345678901,"big":2.3e+500}'
I've been playing about with the uniqid but it started giving me the 13 long string.
I'm looking for a prefix of 100 with up to 6 random numbers afterwards
thanks any help appreciated
function generate_order(){
$order_ref="";
$a=uniqid(prefix,100);
$num4=array('0','1','2','3','4','5','6','7','8','9');
$num=rand(0,9);
$num2=rand(0,9);
$num3=rand(0,9);
shuffle($num4);
//now the final
$order_ref = $num4[0].$num4[3].$num.$num4[1].$num2.$num4[2].$num3.$num4[4];
}
I've got some remakrs to the code above:
Why are you doing a shuffle() on an array with numbers ranging from 1 to 9? This is the same as doing rand(1,9)
Taking point one into account, using rand(10000000, 99999999) will give you the same result.
$a=uniqid(prefix,100) is not used in your function
The prefix in uniqid() should be a string.
Your function doesn't return. You should use return $order_ref;
Hope these will help you fix your function.
In response to your comment
$order_ref = '100'.rand(100000, 999999);
Additional suggestions
If you are planning to use this as an order reference as I suspect, I do not recommand just using random numbers. This will give you a big chance of having duplicate numbers.
Instead, I suggest using $order_ref = '100'.date('u').rand(10, 99);. This will give you a random number based on the current time and thus prevent (or at least minimize) the chance of duplicate order references.
If you want 100 before your result, then use the prefix to enter your desired string
Using uniqid(100) should give you the desired result.
Here is how I would do
1. Define your prefix
2. Generate random number
3. Concat both
$prefix="some_prefix";
$rand_no = rand(1,100);
$rand_no_with_prefix = $prefix.$rand_no;
You can Google for exact syntax and functions.
This is just basic logic I would follow.
Hope it helps.
Also step 4 would be to parse if required(if concated random number needs to be integer)
PHP automatically removes the last zero after the decimal point. Forinstance
$number=29.10;
$echo 'Number='. $number;
This will print
Number = 29.1
instead of
Number = 29.10
I actually need the zero after the decimal place but to no avail.
Why is this so? Any idea or link about this, is very much appreciated.
Thanks
I am trying to make this calculation in php but is giving me wrong result. I think that is right.
And if i do 5000.00 - 100.10 it works, but i want the 5,000.00 to work too.
This is my code:
To create the 5,000.00 i have used number_format(5000, 2).
Aswell to the 100.10
$total = $value1 - $value2;
echo $total;
?>
$total = -95.00
I am trying to make this calculation in php but is giving me wrong result. I think that is right.
And if i do 5000.00 - 100.10 it works, but i want the 5,000.00 to work too.
Please Help...
If you want to do arithmetic on number, you can't have the thousands separator (,). What's happening is 5,000.00 is being read as 5 (it stops interpreting it as a number as soon as it hits the comma) and then you're getting 5 - 100.10 which is -95.10 (I'm thinking you left off the .10 in your example.
You'll need to convert first:
$value1 = floatval(str_replace(',', '', $original_value1))
$value2 = floatval(str_replace(',', '', $original_value2))
I'm assuming here that you have them as strings originally. These remove the comma separator.
It sounds like you're confusing rendering in the UI with calculations.
It's perfectly reasonable for a user to see currencies rendered according to their locale rules (e.g. a String "$1,000.00" in USA), but the calculations in the back need to done on a floating point number (e.g. 1000.0).
So you have to be able to convert back and forth between them. You can't make arithmetic operations work on a String. Better to parse the String to a float, do the operations, then convert that back to String for rendering.
Maybe the question is simple, but I can't find the answer.
What I need to do is to round number to 2 places after comma.
Im using this:
round(($data/$count*100), 2)
And when I get number like:
60.36036036036012 and : 37.83783783783808 is OK, because it's: 60.36 and 37.84
But why this:
1.8018018018018036
Is rounded to this:
1.8000000000000003
How to round always to 2 places, after comma?
You should get 1.8 unless you use something like old PHP version with some sort of related bugs. Still, if you want to see 1.80 you need to format output string, otherwise trailing zero will be stripped by default. The most flexible approach would be to use sprintf() formatting, like this:
$val = 1.8000000000000003;
printf("%.02f", round( $val, 2 ));
which would produce
1.80
The key is "%.02f" which means you want to format (f)loating point value, with two digits after dot, padded with 0 when needed (like this case).
See the sprintf() docs for more about available formatting possibilites.
Use PHP NumberFormatter class http://es.php.net/manual/es/class.numberformatter.php