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 (
"...
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...
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 ...
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
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/...
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 =...
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...
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...
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
#...
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')
...
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...
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 ...
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
...
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 (
"...
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("{:...