@@ -2440,8 +2440,12 @@ defmodule Kernel do
2440
2440
2441
2441
"""
2442
2442
defmacro binary_to_integer ( some_binary ) do
2443
- quote do
2444
- list_to_integer ( binary_to_list ( unquote ( some_binary ) ) )
2443
+ case :proplists . get_value ( :binary_to_integer ,
2444
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2445
+ 2 ->
2446
+ quote do: :erlang . binary_to_integer ( unquote ( some_binary ) )
2447
+ :undefined ->
2448
+ quote do: list_to_integer ( binary_to_list ( unquote ( some_binary ) ) )
2445
2449
end
2446
2450
end
2447
2451
@@ -2455,8 +2459,12 @@ defmodule Kernel do
2455
2459
2456
2460
"""
2457
2461
defmacro binary_to_integer ( some_binary , base ) do
2458
- quote do
2459
- list_to_integer ( binary_to_list ( unquote ( some_binary ) ) , unquote ( base ) )
2462
+ case :proplists . get_value ( :binary_to_integer ,
2463
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2464
+ 2 ->
2465
+ quote do: :erlang . binary_to_integer ( unquote ( some_binary ) , unquote ( base ) )
2466
+ :undefined ->
2467
+ quote do: list_to_integer ( binary_to_list ( unquote ( some_binary ) ) , unquote ( base ) )
2460
2468
end
2461
2469
end
2462
2470
@@ -2469,8 +2477,12 @@ defmodule Kernel do
2469
2477
2470
2478
"""
2471
2479
defmacro binary_to_float ( some_binary ) do
2472
- quote do
2473
- list_to_float ( binary_to_list ( unquote ( some_binary ) ) )
2480
+ case :proplists . get_value ( :binary_to_float ,
2481
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2482
+ 1 ->
2483
+ quote do: :erlang . binary_to_float ( unquote ( some_binary ) )
2484
+ :undefined ->
2485
+ quote do: list_to_float ( binary_to_list ( unquote ( some_binary ) ) )
2474
2486
end
2475
2487
end
2476
2488
@@ -2484,8 +2496,12 @@ defmodule Kernel do
2484
2496
2485
2497
"""
2486
2498
defmacro integer_to_binary ( some_integer ) do
2487
- quote do
2488
- list_to_binary ( integer_to_list ( unquote ( some_integer ) ) )
2499
+ case :proplists . get_value ( :integer_to_binary ,
2500
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2501
+ 2 ->
2502
+ quote do: :erlang . integer_to_binary ( unquote ( some_integer ) )
2503
+ :undefined ->
2504
+ quote do: list_to_binary ( integer_to_list ( unquote ( some_integer ) ) )
2489
2505
end
2490
2506
end
2491
2507
@@ -2499,8 +2515,12 @@ defmodule Kernel do
2499
2515
2500
2516
"""
2501
2517
defmacro integer_to_binary ( some_integer , base ) do
2502
- quote do
2503
- list_to_binary ( integer_to_list ( unquote ( some_integer ) , unquote ( base ) ) )
2518
+ case :proplists . get_value ( :integer_to_binary ,
2519
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2520
+ 2 ->
2521
+ quote do: :erlang . integer_to_binary ( unquote ( some_integer ) , unquote ( base ) )
2522
+ :undefined ->
2523
+ quote do: list_to_binary ( integer_to_list ( unquote ( some_integer ) , unquote ( base ) ) )
2504
2524
end
2505
2525
end
2506
2526
@@ -2514,8 +2534,47 @@ defmodule Kernel do
2514
2534
2515
2535
"""
2516
2536
defmacro float_to_binary ( some_float ) do
2517
- quote do
2518
- list_to_binary ( float_to_list ( unquote ( some_float ) ) )
2537
+ case :proplists . get_value ( :float_to_binary ,
2538
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2539
+ 2 ->
2540
+ quote do: :erlang . float_to_binary ( unquote ( some_float ) )
2541
+ :undefined ->
2542
+ quote do: list_to_binary ( float_to_list ( unquote ( some_float ) ) )
2543
+ end
2544
+ end
2545
+
2546
+ @ doc """
2547
+ Returns a binary which corresponds to the text representation
2548
+ of `some_float`.
2549
+
2550
+ ## Options
2551
+
2552
+ * `:decimals` — number of decimal points to show
2553
+ * `:scientific` — number of decimal points to show, in scientific format
2554
+ * `:compact` — If true, use the most compact representation. Ignored with the `scientific` option
2555
+
2556
+ ## Examples
2557
+
2558
+ float_to_binary 7.1, [decimals: 2, compact: true] #=> "7.1"
2559
+
2560
+ """
2561
+ defmacro float_to_binary ( some_float , options ) do
2562
+ options = :lists . reverse ( :lists . foldl ( fn ( { :compact , false } , acc ) -> acc -- [ :compact ]
2563
+ ( { :compact , true } , acc ) -> [ :compact | acc ]
2564
+ ( x , acc ) -> [ x | acc ]
2565
+ end , [ ] , options ) )
2566
+ case :proplists . get_value ( :float_to_binary ,
2567
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2568
+ 2 ->
2569
+ quote do: :erlang . float_to_binary ( unquote ( some_float ) , unquote ( options ) )
2570
+ :undefined ->
2571
+ case :proplists . get_value ( :float_to_list ,
2572
+ :proplists . get_value ( :exports , :erlang . module_info , [ ] ) ) do
2573
+ 2 ->
2574
+ quote do: list_to_binary ( float_to_list ( unquote ( some_float ) , unquote ( options ) ) )
2575
+ 1 ->
2576
+ throw ( :badarg )
2577
+ end
2519
2578
end
2520
2579
end
2521
2580
0 commit comments