- Object Oriented Style
bool IntlCalendar::set (int $field, int $value)
orbool IntlCalendar::set (int $year, int $month, int $dayOfMonth = NULL, int $hour = NULL, int $minute = NULL, int $second = NULL)
- Procedural style
bool intlcal_set (IntlCalendar $cal, int $field, int $value)
orbool intlcal_set ( IntlCalendar $cal, int $year, int $month, int $dayOfMonth = NULL, int $hour = NULL, int $minute = NULL, int $second = NULL)
- $cal:This parameter contains the IntlCalendar object resource.
- $field:This parameter is Holds one of the constants of the IntlCalendar date / time field. Field constants are integer values ​​and range from 0 to IntlCalendar::FIELD_COUNT.
- $value:This parameter contains the new value for this field.
- $year:this parameter contains a new value for the IntlCalendar::FIELD_YEAR field.
- $month:this parameter contains a new value for the IntlCalendar::FIELD_MONTH field.
- $dayOfMonth:this parameter contains the new value for the IntlCalendar::FIELD_DAY_OF_MONTH field. Month sequence starts at zero, i.e. 0 for January, 1 for February, etc.
- $hour:this parameter contains a new value for the IntlCalendar::FIELD_HOUR_OF_DAY field .
- $minute:this parameter contains the new value for the IntlCalendar::FIELD_MINUTE field.
- $second:this parameter contains new value for the IntlCalendar::FIELD_SECOND field.
// Set DateTime Zone
ini_set
(
’date.timezone’
,
’ Asia / Calcutta’
);
ini_set
(
’date.timezone’
,
’UTC’
);
// Create an IntlCalendar instance
$calendar
= IntlCalendar::createInstance (
’Asia / Calcutta’
);
// Set the DateTime object
$calendar
-> set (2019, 8, 24);
// Show the calendar object
var_dump (IntlDateFormatter::formatObject (
$calendar
));
// Declare a new IntlGregorianCalendar object
$calendar
=
new
IntlGregorianCalendar (2016, 8, 24);
// Set the year field
$calendar
-> set (IntlCalendar::FIELD_YEAR, 2018);
// Show the calendar object
var_dump (IntlDateFormatter::formatObject (
$calendar
));
?>
Exit:string (24 ) "Sep 24, 2019, 8:23:53 AM" string (25) "Sep 24, 2018, 12:00:00 AM"
Link: https://www.php.net/manual/en/intlcalendar.set.php
PHP IntlCalendar set () function: StackOverflow Questions
How to print number with commas as thousands separators?
I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567
as 1,234,567
. How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way.
It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably possible.
Answer #1:
Locale unaware
"{:,}".format(value) # For Python ≥2.7
f"{value:,}" # For Python ≥3.6
Locale aware
import locale
locale.setlocale(locale.LC_ALL, "") # Use "" for auto, or force e.g. to "en_US.UTF-8"
"{:n}".format(value) # For Python ≥2.7
f"{value:n}" # For Python ≥3.6
Reference
Per Format Specification Mini-Language,
The ","
option signals the use of a comma for a thousands separator. For a locale aware separator, use the "n"
integer presentation type instead.
Answer #2:
I got this to work:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US")
"en_US"
>>> locale.format("%d", 1255000, grouping=True)
"1,255,000"
Sure, you don"t need internationalization support, but it"s clear, concise, and uses a built-in library.
P.S. That "%d" is the usual %-style formatter. You can have only one formatter, but it can be whatever you need in terms of field width and precision settings.
P.P.S. If you can"t get locale
to work, I"d suggest a modified version of Mark"s answer:
def intWithCommas(x):
if type(x) not in [type(0), type(0L)]:
raise TypeError("Parameter must be an integer.")
if x < 0:
return "-" + intWithCommas(-x)
result = ""
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
Recursion is useful for the negative case, but one recursion per comma seems a bit excessive to me.
Answer #3:
I"m surprised that no one has mentioned that you can do this with f-strings in Python 3.6+ as easy as this:
>>> num = 10000000
>>> print(f"{num:,}")
10,000,000
... where the part after the colon is the format specifier. The comma is the separator character you want, so f"{num:_}"
uses underscores instead of a comma. Only "," and "_" is possible to use with this method.
This is equivalent of using format(num, ",")
for older versions of python 3.
Answer #4:
For inefficiency and unreadability it"s hard to beat:
>>> import itertools
>>> s = "-1234567"
>>> ",".join(["%s%s%s" % (x[0], x[1] or "", x[2] or "") for x in itertools.izip_longest(s[::-1][::3], s[::-1][1::3], s[::-1][2::3])])[::-1].replace("-,","-")
How would you make a comma-separated string from a list of strings?
Question by mweerden
What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ["a", "b", "c"]
to "a,b,c"
? (The cases ["s"]
and []
should be mapped to "s"
and ""
, respectively.)
I usually end up using something like "".join(map(lambda x: x+",",l))[:-1]
, but also feeling somewhat unsatisfied.
Answer #1:
my_list = ["a", "b", "c", "d"]
my_string = ",".join(my_list)
"a,b,c,d"
This won"t work if the list contains integers
And if the list contains non-string types (such as integers, floats, bools, None) then do:
my_string = ",".join(map(str, my_list))