Decimal commas instead of points

Does Julia provide a simple way to use decimal commas instead of the standard point?

Some countries use that standard and working with local data is sometimes a hassle. For example, importing spreadsheets with percentage numbers would require users to import the string, convert commas into dots, then use parse to get the proper float variable.

The biggest issue for me is for making tables and graphs. Other tools allow you to set some localization variable which will write variables as appropriate, so an internally stored “2.5” can be shown as “2,5” in such situation.

I don’t think there’s a built-in way. You can redefine the method that prints floats if you want

 @eval Base.Ryu begin 
        function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
           compact = get(io, :compact, false)::Bool
           buf = Base.StringVector(neededdigits(T))
           typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
           pos = writeshortest(buf, 1, x, false, false, true, -1,
               (x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8(','), typed, compact)
           write(io, resize!(buf, pos - 1))
           return
        end
end

The UInt8(',') above used to be UInt8('.') in the original.

This carries some risk though, since you don’t know if anything else depends on the expected/default printing, and will silently (or loudly) break. You could also define your own function for printing decimals with commas, either based on the above or custom, and use it when you need it. I.e. when making labels for graphs and tables, use your new function to create the strings.

If this is a must-have for you, you could open an issue on github. Maybe someone who knows more can figure out if making this possible breaks anything important.

1 Like

use CSV.jl:

I thought I had answered before, but apparently something came up. I’m glad to know a solution exists, but it’s a bit sad there’s no simple ‘locale flag’ to set either as default. That said, there would still need additional work for the Plots side of things.

@jling thanks for pointing out CSV.jl. It can probably solve the problem of writing into files with the appropriate limiter, but it certainly doesn’t solve showing in Julia REPL and plotting a graph with decimal commas.

Could you use Printf for some of this?

using Printf
replace(@sprintf("%.4f",π), "." => ",")
"3,1416"

And for plotting, place the ticks manually.

1 Like

I like the simplicity of this approach for dealing with tables, unless Julia has functionality which takes a matrix of elements and automatically turns them into a LaTeX table (which I’m almost sure it does). This may be solved by first translating an array of an arbitrary type into a properly formatted string.

A similar workaround may be possible with Plots. Maybe I can override the current print function to use decimal commas for my environment.

Julia is intentionally locally-independent: make parsing float literals locale-independent · Issue #5928 · JuliaLang/julia · GitHub

There’s however open issue:

You want something like in Python, a locale module:

I’m not aware of any such module for Julia, in a package, nor as a stdlib, but there is for dates (in the docs): “custom locales can be loaded by passing in the locale=>Dict{String,Int} mapping to the MONTHTOVALUEABBR and MONTHTOVALUE dicts for abbreviated and full-name month names, respectively.”

1 Like