mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
float: make default string representations for floats become alike
This commit is contained in:
parent
1c68417918
commit
770132ff37
@ -7,23 +7,22 @@ import strconv.ftoa
|
||||
|
||||
#include <float.h>
|
||||
// ----- f64 to string functions -----
|
||||
// str return a f64 as string in scientific notation, auto display digits limit
|
||||
// str return a f64 as string in suitable notation
|
||||
[inline]
|
||||
pub fn (d f64) str() string {
|
||||
return ftoa.ftoa_64(d)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (d any_float) str() string {
|
||||
x := f64(d)
|
||||
pub fn (x f64) str() string {
|
||||
abs_x := f64_abs(x)
|
||||
if abs_x >= 0.01 && abs_x < 1.0e16 {
|
||||
if abs_x >= 0.0001 && abs_x < 1.0e6 {
|
||||
return ftoa.f64_to_str_l(x)
|
||||
} else {
|
||||
return ftoa.ftoa_64(x)
|
||||
}
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (d any_float) str() string {
|
||||
return f64(d).str()
|
||||
}
|
||||
|
||||
// return a string of the input f64 in scientific notation with digit_num deciamals displayed, max 17 digits
|
||||
[inline]
|
||||
pub fn (x f64) strsci(digit_num int) string {
|
||||
@ -43,10 +42,15 @@ pub fn (x f64) strlong() string {
|
||||
}
|
||||
|
||||
// ----- f32 to string functions -----
|
||||
// str return a f32 as string in scientific notation, auto display digits limit
|
||||
// str return a f32 as string in suitable notation
|
||||
[inline]
|
||||
pub fn (d f32) str() string {
|
||||
return ftoa.ftoa_32(d)
|
||||
pub fn (x f32) str() string {
|
||||
abs_x := f32_abs(x)
|
||||
if abs_x >= 0.0001 && abs_x < 1.0e6 {
|
||||
return ftoa.f32_to_str_l(x)
|
||||
} else {
|
||||
return ftoa.ftoa_32(x)
|
||||
}
|
||||
}
|
||||
|
||||
// return a string of the input f32 in scientific notation with digit_num deciamals displayed, max 8 digits
|
||||
@ -69,7 +73,7 @@ pub fn (x f32) strlong() string {
|
||||
|
||||
// ----- C functions -----
|
||||
[inline]
|
||||
fn f32_abs(a f32) f32 {
|
||||
pub fn f32_abs(a f32) f32 {
|
||||
return if a < 0 {
|
||||
-a
|
||||
} else {
|
||||
@ -88,7 +92,7 @@ fn f64_abs(a f64) f64 {
|
||||
|
||||
|
||||
[inline]
|
||||
fn f32_max(a, b f32) f32 {
|
||||
pub fn f32_max(a, b f32) f32 {
|
||||
return if a > b {
|
||||
a
|
||||
} else {
|
||||
@ -97,7 +101,7 @@ fn f32_max(a, b f32) f32 {
|
||||
}
|
||||
|
||||
[inline]
|
||||
fn f32_min(a, b f32) f32 {
|
||||
pub fn f32_min(a, b f32) f32 {
|
||||
return if a < b {
|
||||
a
|
||||
} else {
|
||||
@ -106,7 +110,7 @@ fn f32_min(a, b f32) f32 {
|
||||
}
|
||||
|
||||
[inline]
|
||||
fn f64_max(a, b f64) f64 {
|
||||
pub fn f64_max(a, b f64) f64 {
|
||||
return if a > b {
|
||||
a
|
||||
} else {
|
||||
|
@ -21,8 +21,17 @@ fn test_float_lit_call_method() {
|
||||
x4 := .003e2.str()
|
||||
assert x4 == '0.3'
|
||||
x5 := 2.e-3.str()
|
||||
assert x5 == '2.e-03'
|
||||
assert x5 == '0.002'
|
||||
x6 := 5.0.str()
|
||||
assert x6 == '5.'
|
||||
// x7 := 5..str() Syntax `5.` is allowed, but do not call method on it (`5..str()` is parsed as a range). Use `5.0.str()` instead.
|
||||
x8 := 7.345e-7.str()
|
||||
assert x8 == '7.345e-07'
|
||||
x9 := 5.725e6.str()
|
||||
assert x9 == '5.725e+06'
|
||||
|
||||
a := f32(12.3).str()
|
||||
assert a[0..4] == '12.3' // there is still trailing garbage
|
||||
assert f32(7.345e-7).str() == '7.345e-07'
|
||||
assert f32(5.725e6).str() == '5.725e+06'
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user