diff --git a/src/write.jl b/src/write.jl index 3a67c55e..b0b14108 100644 --- a/src/write.jl +++ b/src/write.jl @@ -454,9 +454,16 @@ function writecell(buf, pos, len, io, x::AbstractFloat, opts) bytes = codeunits(string(x)) sz = sizeof(bytes) @check sz - for i = 1:sz - @inbounds buf[pos] = bytes[i] - pos += 1 + if opts.decimal != UInt8('.') + for i = 1:sz + @inbounds buf[pos] = bytes[i] == UInt8('.') ? opts.decimal : bytes[i] + pos += 1 + end + else + for i = 1:sz + @inbounds buf[pos] = bytes[i] + pos += 1 + end end return pos end diff --git a/test/write.jl b/test/write.jl index 314e18a7..a9f683f3 100644 --- a/test/write.jl +++ b/test/write.jl @@ -23,6 +23,10 @@ struct StructType aumber::Union{Real, Nothing} end +struct AF <: AbstractFloat + f::Float64 +end +Base.string(x::AF) = string(x.f) @testset "CSV.write" begin @@ -156,6 +160,12 @@ end (delim='\t', decimal=','), "col1\tcol2\tcol3\n1,1\t4\t7\n2,2\t5\t8\n3,3\t6\t9\n" ), + # custom abstract float decimal: #1108 + ( + (col1=AF.([1.1,2.2,3.3]), col2=[4,5,6], col3=[7,8,9]), + (delim='\t', decimal=','), + "col1\tcol2\tcol3\n1,1\t4\t7\n2,2\t5\t8\n3,3\t6\t9\n" + ), # issue 515 ( (col1=[""],),