2017-04-21 86 views
6

什麼是最直接的,&在Elixir中做到這一點的有效方法?如何在Elixir中對數字進行精確格式化?

Starting number: 123.101 

Ending number: 123.101000 # Adding 3 digits to the precision of a float. 

Starting number: 123 

Ending number: 123.000 # Adding 3 digits to the precision of an integer. 

Starting number: 123.101 

Ending number: 123.1 # removing precision 

Starting number: 123.000 

Ending number: 123 # removing precision 

回答

4

沒有在藥劑的標準庫這樣的事,據我知道,但你可以使用從二郎:io_lib.format/2點後打印與數字的具體數量浮動:

iex(1)> :io_lib.format("~.3f", [123.101]) 
['123.101'] 
iex(2)> :io_lib.format("~.6f", [123.101]) 
['123.101000'] 
iex(3)> :io_lib.format("~.3f", [123.0]) 
['123.000'] 
iex(4)> :io_lib.format("~.6f", [123.0]) 
['123.000000'] 

返回值是一個iolist,其可根據需要(即採取二進制可以直接接受iolist以及許多功能,例如IO.puts/1)被轉換爲使用IO.iodata_to_binary/1二進制:

iex(5)> :io_lib.format("~.6f", [123.0]) |> IO.iodata_to_binary 
"123.000000" 

~.0f不工作,但對於您可以簡單地調用trunc/1

iex(6)> trunc(123.123) 
123 
1

我用過的decimal軟件包這一先前

iex(6)> 123 |> Decimal.new() |> Decimal.round(3) |> Decimal.to_string() 
"123.000" 

在我的情況下,數據已經在從數據庫查詢後的十進制格式。

14

只是想提供一個替代Dogbert的優秀答案。

也可以使用:erlang.float_to_binary/2

前。

iex(5)> :erlang.float_to_binary(123.101, [decimals: 6]) 
"123.101000" 

iex(6)> :erlang.float_to_binary(123.0, [decimals: 3]) 
"123.000" 

iex(7)> :erlang.float_to_binary(123.101, [decimals: 1]) 
"123.1" 

iex(8)> :erlang.float_to_binary(123.000, [decimals: 0]) 
"123" 
+1

這比我的回答更好!我檢查了'Float.to_string',但它沒有任何選項,我忘記檢查Erlang中的'float_to_ *'函數。 – Dogbert

1

只是出於好奇,這就是我將如何實現在純藥劑:

defmodule NumFmt do 
    def format(value, pos, round? \\ true) 
    def format(value, pos, _) when is_integer(value), 
    do: format(Integer.to_string(value), pos) 
    def format(value, pos, round?) when is_float(value), 
    do: format(Float 
       |> apply((if round?, do: :round, else: :floor), [value, pos]) 
       |> Float.to_string, pos) 
    def format(value, 0, _) when is_binary(value), 
    do: with [i | _] <- String.split(value, "."), do: i 
    def format(value, pos, round?) when is_binary(value) do 
    case String.split(value, ".") do 
    [i] -> format(i <> ".0", pos, round?) 
    [i, f] -> [i, f 
        |> String.pad_trailing(pos, "0") 
        |> String.slice(0..pos - 1)] |> Enum.join(".") 
    end 
    end 
end 

IO.inspect NumFmt.format(123.101, 6), label: "123.101000" 
#⇒123.101000: "123.101000" 

IO.inspect NumFmt.format(123, 3), label: "123.000" 
#⇒ 123.000: "123.000" 

IO.inspect NumFmt.format(123.101, 1), label: "123.1" 
#⇒ 123.1: "123.1" 

IO.inspect NumFmt.format(123.000, 0), label: "123" 
#⇒ 123: "123" 
2

這些答案大多是奇數。首先,她沒有說任何關於轉換爲字符串的內容。其次,肯定不需要跳入二郎地!

爲了:

Starting number: 123.101 

Ending number: 123.101000 # Adding 3 digits to the precision of a float. 

iex> 123.101 |> Decimal.new() |> Decimal.round(6) 
#Decimal<123.101000> 


Starting number: 123 

Ending number: 123.000 # Adding 3 digits to the precision of an integer. 

iex> 123 |> Decimal.new() |> Decimal.round(3)  
#Decimal<123.000> 


Starting number: 123.101 

Ending number: 123.1 # removing precision 

iex> 123.101 |> Decimal.new() |> Decimal.round(1) 
#Decimal<123.1> 


Starting number: 123.000 

Ending number: 123 # removing precision 

iex> 123.000 |> Decimal.new() |> Decimal.round(0) 
#Decimal<123> 

一旦你有一個Decimal數量,你可以做你想做的,例如轉換爲浮動,轉換爲字符串,等等

iex> 123.101 |> Decimal.new() |> Decimal.round(6) |> Decimal.to_string() 
"123.101000" 

iex> 123.101 |> Decimal.new() |> Decimal.round(6) |> Decimal.to_float() 
123.101 

iex> 123.101 |> Decimal.new() |> Decimal.round(0) |> Decimal.to_integer() 
123 

順便說一句,我建議只在Decimal工作,直到你完成所有的數據操作,例如使用Decimal.mult(num1, num2),Decimal.div(num1, num2)Decimal岩石!

相關問題