ddonche/goblin-lang
0.46.24
1
0
docs reference
[[clear-format]]

Clear_format


clear_format removes display formatting from a formatted number and returns its underlying numeric value.


Overview

Use clear_format when you want the original number without its attached display rules:

price | 1234.5.format(2, ",", ".")
plain_price | price.clear_format()

:say(price)        /// 1,234.50
:say(plain_price)  /// 1234.5
price | 1234.5.format(2, ",", ".")
plain_price | price.clear_format()

:say(price)        /// 1,234.50
:say(plain_price)  /// 1234.5

Formatting controls how the number is displayed. clear_format removes that formatting metadata without changing the number itself.


Call Styles

clear_format supports method style:

plain_price | price.clear_format()
plain_price | price.clear_format()

It can also be called as a free function:

plain_price | clear_format(price)
plain_price | clear_format(price)

Both forms return the same value.


Preserves the Numeric Type

Clearing formatting does not cast the number. The underlying numeric type remains the same:

count | 1200.format(0, ",", ".")
plain_count | count.clear_format()

:say(count)           /// 1,200
:say(plain_count)     /// 1200
:say(plain_count.vt)  /// int
count | 1200.format(0, ",", ".")
plain_count | count.clear_format()

:say(count)           /// 1,200
:say(plain_count)     /// 1200
:say(plain_count.vt)  /// int

The same applies to formatted float, big, and pct values.


Does Not Change the Original

clear_format returns a new unformatted result. It does not remove formatting from the original binding:

distance | 9876.5.format(1, "_", ".")
plain_distance | distance.clear_format()

:say(distance)        /// 9_876.5
:say(plain_distance)  /// 9876.5
distance | 9876.5.format(1, "_", ".")
plain_distance | distance.clear_format()

:say(distance)        /// 9_876.5
:say(plain_distance)  /// 9876.5

Retether the variable when you want to replace the formatted value:

distance |= distance.clear_format()
distance |= distance.clear_format()


Values Without Formatting

When the value has no formatting attached, clear_format returns it unchanged:

name | "Goblin"
same_name | name.clear_format()

:say(same_name)  /// Goblin
name | "Goblin"
same_name | name.clear_format()

:say(same_name)  /// Goblin

This pass-through behavior applies to any unformatted value.


Checking the Result

Use Format Info to inspect a value's formatting metadata:

score | 1234.5.format(2, ",", ".")
plain_score | score.clear_format()

:say(format_info(score))        /// {dec: 2, th: ,, decmark: .}
:say(format_info(plain_score))  /// nil
score | 1234.5.format(2, ",", ".")
plain_score | score.clear_format()

:say(format_info(score))        /// {dec: 2, th: ,, decmark: .}
:say(format_info(plain_score))  /// nil

After clear_format, the returned number has no format information.


Signature

value.clear_format()
clear_format(value)
value.clear_format()
clear_format(value)

Argument Type Description
value any The value whose formatting should be removed.

Returns the underlying number when value is formatted. Otherwise, returns value unchanged.


Errors

clear_format requires exactly one value. In method style, the receiver supplies that value:

clear_format()
/// error: R0301 wrong-arity

clear_format(12, 34)
/// error: R0301 wrong-arity
clear_format()
/// error: R0301 wrong-arity

clear_format(12, 34)
/// error: R0301 wrong-arity

Passing an unformatted or non-numeric value is not an error; the value is returned unchanged.