Round

Round( value, [ precision ]  )
Round( value, [ precision ]  )
Round( value, [ precision ]  )
Round( value, [ precision ]  )
round( value, [ precision ]  )
Round( value, [ precision ]  )

Description

The Round function returns a number which is the nearest mathematic value of the operand to the specified precision as a double value.

Parameters

Argument Type Definition and Requirements

value

A number to be rounded with a specific precision.

precision

Optional - Defines how many digits to the right or left of the decimal place should be returned. The default precision is 2 which returns up to the hundredths decimal places. A positive precision specifies digits to the right of the decimal point. A negative precision specifies digits to the left of the decimal point. A zero precision rounds the fractional part of the number.

Since Go does not have function overloading or optional arguments, you must use the function f.Precision() instead of a plain number.

Returns

A number with its precision modified.

Examples

The following query executes an array of independent rounding operations and returns the results in an array. The result array position matches the position of the execution array position. The first operation takes the value 1234.5678 and by default rounds the value to 2 digits to the right of the decimal point. The result is 1234.57 which is in the top position of the result array. The second operation rounds the value 1234 to a precision of -2. This is two digits to the left of the decimal point, the hundreds position. The operations to the left of the decimal point are zero filled less than the specified precision. The result of 1200 is placed in the third position of the result array.

Value result = await client.Query(
    Arr(
        Round(1234.5678),
        Round(1234, -2),
        Round(1234.5678, 2),
        Round(1234.5678, -2),
        Round(5678.1234, 3),
        Round(1234, -3),
        Round(5678.1234, -3)
    )
);

Console.WriteLine(result);
Arr(DoubleV(1234.57), LongV(1200), DoubleV(1234.57), DoubleV(1200), DoubleV(5678.123), LongV(1000), DoubleV(6000))
result, err := client.Query(
	f.Arr{
		f.Round(1234.5678),
		f.Round(1234, f.Precision(-2)),
		f.Round(1234.5678, f.Precision(2)),
		f.Round(1234.5678, f.Precision(-2)),
		f.Round(5678.1234, f.Precision(3)),
		f.Round(1234, f.Precision(-3)),
		f.Round(5678.1234, f.Precision(-3)),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[1234.57 1200 1234.57 1200 5678.123 1000 6000]
System.out.println(
    client.query(
        Arr(
            Round(1234.5678),
            Round(1234L, -2L),
            Round(1234.5678, 2L),
            Round(1234.5678, -2L),
            Round(5678.1234, 3L),
            Round(1234L, -3L),
            Round(5678.1234, -3L)
        )
    ).get());
[1234.57, 1200, 1234.57, 1200.0, 5678.123, 1000, 6000.0]
client.query(
  [
    q.Round(1234.5678),
    q.Round(1234, -2),
    q.Round(1234.5678, 2),
    q.Round(1234.5678, -2),
    q.Round(5678.1234, 3),
    q.Round(1234, -3),
    q.Round(5678.1234, -3),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
[
   1234.57, 1200,
   1234.57, 1200,
  5678.123, 1000,
      6000
]
result = client.query(
  [
    q.round(1234.5678),
    q.round(1234, -2),
    q.round(1234.5678, 2),
    q.round(1234.5678, -2),
    q.round(5678.1234, 3),
    q.round(1234, -3),
    q.round(5678.1234, -3)
  ]
)
print(result)
[1234.57, 1200, 1234.57, 1200.0, 5678.123, 1000, 6000.0]
println(Await.result(
    client.query(
        Arr(
            Round(1234.5678),
            Round(1234, -2),
            Round(1234.5678, 2),
            Round(1234.5678, -2),
            Round(5678.1234, 3),
            Round(1234, -3),
            Round(5678.1234, -3)
        )
    ),
    5.seconds
))
[1234.57, 1200, 1234.57, 1200.0, 5678.123, 1000, 6000.0]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!