More human readable $(float)

The output matches that of Python (eg 1e100, not 1.0e100), but also
reflects locale (assuming it was set using setlocale() before).
This commit is contained in:
katlogic 2014-06-15 01:49:14 +02:00
commit cea2a9087d
3 changed files with 16 additions and 7 deletions

View file

@ -250,10 +250,16 @@ proc nimIntToStr(x: int): string {.compilerRtl.} =
for j in 0..i div 2 - 1:
swap(result[j], result[i-j-1])
proc nimFloatToStr(x: float): string {.compilerproc.} =
var buf: array [0..59, char]
c_sprintf(buf, "%#.16e", x)
return $buf
proc nimFloatToStr(f: float): string {.compilerproc.} =
var buf: array [0..64, char]
var n:int = c_sprintf(buf, "%.16g", f)
for i in 0..n-1:
if buf[i] notin {'0'..'9','-'}:
return $buf
buf[n] = c_localeconv()[0]
buf[n+1] = '0'
buf[n+2] = '\0'
result = $buf
proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
result = newString(sizeof(x)*4)