@@ -291,23 +291,51 @@ impl Number {
291291 ///
292292 /// The `toLocaleString()` method returns a string with a language-sensitive representation of this number.
293293 ///
294- /// Note that while this technically conforms to the Ecma standard, it does no actual
295- /// internationalization logic.
296- ///
297294 /// More information:
298295 /// - [ECMAScript reference][spec]
299296 /// - [MDN documentation][mdn]
300297 ///
301- /// [spec]: https://tc39.es/ecma262/#sec -number.prototype.tolocalestring
298+ /// [spec]: https://tc39.es/ecma402/#sup -number.prototype.tolocalestring
302299 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
303300 #[ allow( clippy:: wrong_self_convention) ]
301+ #[ allow(
302+ unused_variables,
303+ reason = "`args` and `context` are used if the `intl` feature is enabled"
304+ ) ]
304305 pub ( crate ) fn to_locale_string (
305306 this : & JsValue ,
306- _ : & [ JsValue ] ,
307- _ : & mut Context ,
307+ args : & [ JsValue ] ,
308+ context : & mut Context ,
308309 ) -> JsResult < JsValue > {
309- let this_num = Self :: this_number_value ( this) ?;
310- Ok ( JsValue :: new ( js_string ! ( this_num) ) )
310+ // Let x be ? thisNumberValue(this value).
311+ let x = Self :: this_number_value ( this) ?;
312+
313+ #[ cfg( feature = "intl" ) ]
314+ {
315+ use fixed_decimal:: { Decimal , FloatPrecision } ;
316+
317+ use crate :: builtins:: intl:: NumberFormat ;
318+
319+ if !x. is_finite ( ) {
320+ return Ok ( JsValue :: new ( js_string ! ( x) ) ) ;
321+ }
322+
323+ let locales = args. get_or_undefined ( 0 ) . clone ( ) ;
324+ let options = args. get_or_undefined ( 1 ) . clone ( ) ;
325+
326+ // Let numberFormat be ? Construct(%Intl.NumberFormat%, « locales, options »).
327+ let number_format = NumberFormat :: new ( & locales, & options, context) ?;
328+ let mut x = Decimal :: try_from_f64 ( x, FloatPrecision :: RoundTrip )
329+ . map_err ( |err| JsNativeError :: range ( ) . with_message ( err. to_string ( ) ) ) ?;
330+
331+ // Return FormatNumeric(numberFormat, ℝ(x)).
332+ Ok ( js_string ! ( number_format. format( & mut x) . to_string( ) ) . into ( ) )
333+ }
334+
335+ #[ cfg( not( feature = "intl" ) ) ]
336+ {
337+ Ok ( JsValue :: new ( js_string ! ( x) ) )
338+ }
311339 }
312340
313341 /// `flt_str_to_exp` - used in `to_precision`
0 commit comments