Number, date, time

Source: Internet
Author: User
Tags timedelta

1, we want to take a floating point number to the fixed decimal place

Use the built-in round (value,ndigits) to:

1 >>> round (1.23,1)2 1.23 >>> round (1.27,1)4 1.35 >>> round ( -1.27,1)6 -1.37 >>> round (1.1231237,3 )8 1.1239

Ndigits can be negative, in which case it will be rounded to 10 bits, hundreds of thousands, etc., such as:

1 >>> round (1.1231237,3)  2 1.123 3 >>> a = 1231231 4 >& Gt;> round (a,-1)  5 1231230 6 >>> round (a,-2)  7 1231200 8< /c10> >>> Round (a,-3)  9 1231000

The output of the value can not be confused with the formatted output, if only to let the value of a fixed number of digits output, generally do not round ():

1>>> x = 1.234562>>> Format (x,'0.2f')3 '1.23'4>>> Format (x,'0.3f')5 '1.235'6>>>'value is {: 0.3f}'. Format (x)7 'value is 1.235'

To perform a precise decimal operation, let's take a look at the following small example:

1 >>> a = 4.22 >>> B = 2.13 >>> A + b4 6.300 0000000000015 >>> (A + b) = = 6.36False7

Actually false, these errors are essentially a feature of the underlying CPU's floating-point unit and the IEEE754 floating-point arithmetic standard, so how to avoid such errors and get higher accuracy (sacrifice a bit of performance), you can use the decimal module

1>>> fromDecimalImportDecimal2>>> a = Decimal (4.2)3>>> B = Decimal (2.1)4>>> A +b5Decimal ('6.300000000000000266453525910')6>>>Print(A +b)76.3000000000000002664535259108>>> A = Decimal ('4.2')9>>> B = Decimal ('2.1')Ten>>> A +b OneDecimal ('6.3') A>>>Print(A +b) -6.3 ->>> (A + b) = = 6.3 the False ->>> (A + b) = = Decimal ('6.3') - True ->>>

Note quotation marks

Time Conversion

#_ *_coding:utf-8_*_
Import time
Import datetime

Print (Time.clock ()) #返回处理器时间, 3.3 started obsolete
Print (Time.process_time ()) #返回处理器时间, 3.3 started obsolete
Print (Time.time ()) #返回当前系统时间戳
Print (Time.ctime ()) #输出Tue Jan 26 18:23:48 2016, current system time
Print (Time.ctime (Time.time () -86640)) #将时间戳转为字符串格式
Print (Time.gmtime (Time.time () -86640)) #将时间戳转换成struct_time格式
Print (Time.localtime (Time.time () -86640)) #将时间戳转换成struct_time格式, but the local time returned
Print (Time.mktime (Time.localtime ())) #与time. LocalTime () function instead, turn struct_time format back into timestamp format
#time. Sleep (4) #sleep
Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ())) #将struct_time格式转成指定的字符串格式
Print (Time.strptime ("2016-01-28", "%y-%m-%d")) #将字符串格式转换成struct_time格式

#datetime Module

Print (Datetime.date.today ()) #输出格式 2016-01-26
Print (Datetime.date.fromtimestamp (Time.time () -864400)) #2016-01-16 turn the timestamp into a date format
Current_time = Datetime.datetime.now () #
Print (current_time) #输出2016-01-26 19:04:30.335935
Print (Current_time.timetuple ()) #返回struct_time格式

#datetime. replace ([year[, month[, day[, hour[, minute[, second[, microsecond[, Tzinfo] []] []])
Print (Current_time.replace (2014,9,12)) #输出2014-09-12 19:06:24.074900, returns the current time, but the specified value is replaced

Str_to_date = Datetime.datetime.strptime ("21/11/06 16:30", "%d/%m/%y%h:%m") #将字符串转换成日期格式
New_date = Datetime.datetime.now () + Datetime.timedelta (days=10) #比现在加10天
New_date = Datetime.datetime.now () + Datetime.timedelta (days=-10) #比现在减10天
New_date = Datetime.datetime.now () + Datetime.timedelta (hours=-10) #比现在减10小时
New_date = Datetime.datetime.now () + Datetime.timedelta (seconds=120) #比现在 +120s
Print (new_date)

Number, date, time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.