All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Format a Currency with Commas in Golang

Formatting float to currency string in Go. Using localized formatting Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR. Make a main.go file containing the following: package main import ( "...
Unused

How to Convert the Fahrenheit to Celsius in Golang

Calculate degrees celcius from farenheit in Go. Following program shows you how to convert fahrenheit to celsius. The function FToC below encapsu- lates the temperature conversion logic so that it is defined only once but may be used from multiple p...
Sambhav Khandelwal

How to Add Two Numbers in Go

An example program using numbers. Integers And Floating Point Numbers Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers. Make a main.go file containing the ...
pooriabt

How to Get a Random Number Between a Float Range in Python

In Python, using the uniform function is the easiest way to get a random number between a float range. Using uniform Method The random.uniform(a, b) method returns a random floating point number N such that a
Unused

How to Extract All the Numbers Contained in a String in Python

In Python, there are 2 ways to extract all the numbers contained in a string. Using List Comprehension List comprehensions provide a concise way to create lists. If you only want to extract only positive integers, try the following: #!/usr/bin/...
Sambhav Khandelwal

How to Get Current Time in Milliseconds in Python

In Python, there are 3 ways to get current time in milliseconds. Using time Method The time.time() method returns the time in seconds since the epoch as a floating point number. For example, #!/usr/bin/python3 # Import module import time ms =...
Patcher56

How to Convert Float to Integer in Rust

In Rust, there are 2 ways to convert float to integer. Using as Keyword Executing an as expression casts the value on the left-hand side to the type on the right-hand side. An example of an as expression: fn main() { let a = 3.6415_f64; l...
pooriabt

How to Raise a Number to a Power in Rust

In Rust, there are 2 ways to raise a number to a power. Using pow Method The pow() function raises a value to the power of exp, using exponentiation by squaring. Note that 0⁰ (pow(0, 0)) returns 1. Mathematically this is undefined. See the followi...
ada

How to Subtract a Day from a Date in Python

In Python, using the timedelta object is the easiest way to subtract a day from a date. Using timedelta Object A timedelta object represents a duration, the difference between two dates or times. See the following example: #!/usr/bin/python3 #...
Sambhav Khandelwal

How to Check for NaN Values in Python

In Python, there are 2 ways to check for NaN values. Using math.isnan Method The math.isnan(x) method returns True if x is a NaN (not a number), and False otherwise. For example, #!/usr/bin/python3 # Import module import math f = float('nan') ...
ada

How to Check If an Object Is of a Given Type in Python

In Python, there are 2 ways to check if an object is of a given type. Using isinstance Method The isinstance(object, classinfo) method returns True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtua...
Unused

How to Convert an Integer to a Float Number in Go

In Golang, using the float64 function is the easiest way to convert an integer to a float number. Using float64 Function The easiest way to convert an integer to a float number in Golang: package main import "fmt" func main() { i := 6 f ...
Patcher56

How to Generate Random Integers Between 0 and 9 in Python

In Python, there are 3 ways to generate random integers between 0 and 9. Using randrange Method The random.randrange(start, stop[, step]) method returns a randomly selected element from range(start, stop, step). For example, #!/usr/bin/python3 ...
Patcher56

How to Get the Maximum Value for an Int Type in Go

In Golang, there are 2 ways to get the maximum value for an int type. Using constant values Since integer types use two's complement arithmetic, you can infer the min/max constant values for int and uint. For example, package main import ( "...
Patcher56

How to Limit Floats to Two Decimal Points in Python

In Python, there are 4 ways to limit floats to two decimal points. Using format Method You can use str.format() function to limit floats, it really simple. For example, #!/usr/bin/python3 # -*- coding: utf8 -*- f = 13.949999999999999 print("{:...
Unused