I have this number:
$total = '0.04149951583898188';
I want to diplay only the first 8 digits after the dot, like this: 0.0414995
How can I do this?
Here is my answer, guessing from your profile & the code snippet that you’re probably talking about PHP. Do not forget to tell us which language you want help with, we shouldn’t have to guess!
Have a look at PHP’s round() function:
<?php
$total = '0.04149951583898188';
echo round((float)$total, 8); // Cast the $total string variable to a type float, and round it
?>
Related
I would like remove dot from string.
For example
242.701000393 = 242701000393
I have tried below code which is working fine in some cases.
string.gsub("242.701000393", "%.", "")
Same way, I have tried above function for 100999212.707000393 .But it's not working for it.
Am newbie in lua.I would like to just remove .(dot) from string in every cases.
Share your thoughts as i don't know how to achieve it.
As per my logic looks like below
split a string by dot and convert into an array
concat all array elements
Share it solution if possible.
Thanks in advance.
CODE :
local destination_number =100999212.707000393
destination_number = string.gsub(destination_number, "%.", "")
print(destination_number)
Output : 100999212707
Expected output : 100999212707000393
The issue is the accuracy of the number - floating point rounding, not the function of gsub.
local destination_number =100999212.707000393
print(destination_number, type(destination_number) )
destination_number = string.gsub(destination_number, "%.", "")
print(destination_number,type(destination_number))
output
100999212.707 number
100999212707 string
compared to ...
local destination_number = "100999212.707000393"
print(destination_number, type(destination_number) )
destination_number = string.gsub(destination_number, "%.", "")
print(destination_number,type(destination_number))
output
100999212.707000393 string
100999212707000393 string
A floating point double has about 15 digits of accuracy, which means the 393 is being lost in the generation of the number. When converted to a string, it has already gone.
15 digits is quite accurate, and normally good enough for most purposes, but if it is insufficient for you, you will need to consider alternative data representations.
I'm trying to format a currency value with Zend's CurrencyFormat (using ICU). Basically it's working - the pattern #,##0.# outputs a correct format like
1.200,00 €.
Is it possible to leave out the minor unit part if it's zero just by modifying the pattern? I want to have the following formatting results:
1.200 € if minor units are ".00"
1.200,34 € if minour units are not ".00"
I don't think it is possible to achieve it by modifying pattern. I mean, you can check if provided number is type of float or int. If it's int you can set different pattern, but there is simpler way (more recommended way IMO).
3rd argument of __invoke() method is $showDecimal. It takes bool value. If you want decimals to be visible- pass true (it's default value), false otherwise.
Example
true - if number is integer
<?php echo $this->currencyFormat(1234, "EUR", true, "de_DE"); ?>
Output:
1.234,00 €
false - if number is float
<?php echo $this->currencyFormat(1234, "EUR", false, "de_DE"); ?>
Output:
1.234 €
Loop example
// $prices = [1234, 1234.23, 234, 3456.54]
<?php foreach($prices as $price): ?>
<?php $showDecimal = is_float($price) ? true : false;
<p><?php echo $this->currencyFormat(1234, "EUR", $showDecimal, "de_DE"); ?></p>
<?php endforeach; ?>
Global
If for some reason you want hide decimal numbers for all currencies/formats, you can use setShouldShowDecimals() method:
$this->plugin("currencyformat")->setShouldShowDecimals(false)->setCurrencyCode("USD")->setLocale("en_US");
echo $this->currencyFormat(1234.00); // "$1,234"
Here is a list of all available symbols for pattern http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#details
I have a field called 'UCN' which has 6 character.
This Field can have both Character and Numeric Value like "A123Y5" or "12345Y" or "G23561" some thing Like this.
We need to print the data from here with Pipe as A|1|2|3|Y|5.
I am able to put Integer with 'using' keyword, but unable to put both together.
Please if anyone can help
Mukesh
I don't think there's a short cut. You need:
PRINT ucn[1], '|', ucn[2], '|', ucn[3], '|', ucn[4], '|', ucn[5], '|', ucn[6]
If it was marginally longer, you might use a loop instead; that has its own fiddliness.
If you are working for who I think you are working for, I can give you an answer using some Genero extensions to 4GL. Create a library function like ...
FUNCTION insert_between_each_char(str,delimiter)
DEFINE str STRING
DEFINE delimiter CHAR(1)
DEFINE sb base.StringBuffer
DEFINE i INTEGER
LET sb = base.StringBuffer.create()
CALL sb.append(str)
FOR i = sb.getLength() TO 2 STEP -1
CALL sb.insertAt(i,delimiter)
END FOR
RETURN sb.toString()
END FUNCTION
... and then your code becomes
PRINT insert_between_each_char(ucn,"|")
Here is the code to loop like mentioned by Jonathan:
DEFINE
l_result char(512),
l_sel LIKE table.UCN,
i integer
LET l_sel = "A123Y5" #Or select into l_sel
FOR i = 1 to length(l_sel)
IF i < length(l_sel)
THEN
LET l_result = l_result, l_sel[i], "|"
END IF
END FOR
PRINT l_result
I want to update a text like "Updating report (1 0f 5)". I thought format function will help me to do that. I want something like this
string := Format('Updating report ( %d of %d, [1], [2])', loop, count );
but it is not possible. I have an option to have loop and count stored in a string and concatenate everything. But is there any other way to achieve what i want?
Your syntax is wrong. The second parameter to the Format is an open array containing the arguments. So you need to wrap your list of arguments in what is known as an open array constructor.
An open array constructor is a sequence of expressions separated by commas and enclosed in brackets.
So, write the code like this:
str := Format('Updating report (%d of %d)', [loop, count]);
python NNTPLib is giving me author name such as ,
"=?Utf-8?B?RGVubmlzIEJhc2hhbQ==?= < someone#someforum.com >"
(Quotes for clarity).
How do i encode this text in human readable format?
The double equals at the end was a dead giveaway. Base 64 uses that for padding strings that do not have an even multiple of 4 bytes.
In python:
print "RGVubmlzIEJhc2hhbQ==".decode('base_64')
In PHP:
<?php echo base64_decode("RGVubmlzIEJhc2hhbQ=="); ?>
Enjoy.
PS... Dennis Basham