redef class Float
# Pretty print self, print needed decimals up to a max of 6.
redef fun to_s do
- var str = to_precision( 6 )
+ var str = to_precision( 3 )
var len = str.length
for i in [0..len-1] do
var j = len-1-i
end
# `self' representation with `nb' digits after the '.'.
- fun to_precision(nb: Int): String import String::from_cstring `{
+ fun to_precision(nb: Int): String
+ do
+ if nb == 0 then return self.to_i.to_s
+ var f = self
+ for i in [0..nb[ do f = f * 10.0
+ if self > 0.0 then
+ f = f + 0.5
+ else
+ f = f - 0.5
+ end
+ var i = f.to_i
+ if i == 0 then return "0.0"
+ var s = i.to_s
+ var sl = s.length
+ if sl > nb then
+ var p1 = s.substring(0, s.length-nb)
+ var p2 = s.substring(s.length-nb, nb)
+ return p1 + "." + p2
+ else
+ return "0." + ("0"*(nb-sl)) + s
+ end
+ end
+
+ fun to_precision_native(nb: Int): String import String::from_cstring `{
int size;
char *str;
144
sleeping 1 second...
-0.463648
+0.464
from C: self + fib(self) = 29
from Nit: self + fib(self) = 29
test_attr_easy.nit:20,5--16: Warning: Expression is already a String.
test_attr_easy.nit:20,20--26: Warning: Expression is already a B.
test_attr_easy.nit:20,44--62: Warning: Expression is already a HashMap[Int, B].
-true false 12345 1.234499 asdf true
+true false 12345 1.234 asdf true
an instance of B
true true true true
true true true true
test_attr_easy.nit:20,5--16: Warning: Expression is already a String.
test_attr_easy.nit:20,20--26: Warning: Expression is already a B.
test_attr_easy.nit:20,44--62: Warning: Expression is already a HashMap[Int, B].
-true false 12345 1.2345 asdf true
+true false 12345 1.235 asdf true
an instance of B
true true true true
true true true true
-5.12345
+5.123
C implementation!
in C!
42
5.500000
0.550000
-12.345678
-12.345678
-12.345
+12.346
+12.346
+12.346
12.3
12
+++ /dev/null
-5.500000
-0.550000
-12.345679
-12.345679
-12.346
-12.3
-12
-{"int":"1234","float":"0.1234","str":"str","null": null}
+{"int":"1234","float":"0.123","str":"str","null": null}
{"int":1234,"float":0.123400,"str":"str","null":null}
-{"arr":"123","obj":"{"int":"123","float":"-234.449997"}"}
+{"arr":"123","obj":"{"int":"123","float":"-234.45"}"}
{"arr":[1,2,3],"obj":{"int":123,"float":-234.449997}}
-{"arr":"12.3str","obj":"{"int":"123","float":"-234.449997"}"}
+{"arr":"12.3str","obj":"{"int":"123","float":"-234.45"}"}
{"arr":[1,2.300000,null,"str"],"obj":{"int":123,"float":-234.449997}}
-3.141593
+3.142
-1.0
--0.0
0.0
-0.785398
0.0
-1.570796
+0.785
+0.0
+1.571
5.0
1.0
0.0
-0.123450
+0.123
1234.0
-0.989897
+0.99
+++ /dev/null
-0.12345
-1234.0
-0.989898