www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Return the complete number

reply Greatsam4sure <greatsam4sure gmail.com> writes:
int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",&num);
  Writeln(num);

}

How do I return the complete number the user enter since I don't 
since the user can enter numbers of various length with dmd 
approximating it.

I will appreciate any help
Jul 24 2019
next sibling parent drug <drug2004 bk.ru> writes:
24.07.2019 18:45, Greatsam4sure пишет:
 int main(){
   double mum = 0;
   Write("enter a number: ")
   readf(" %s\n",&num);
   Writeln(num);
 
 }
 
 How do I return the complete number the user enter since I don't since 
 the user can enter numbers of various length with dmd approximating it.
 
 I will appreciate any help
does it help? writefln("%.8f", num); // for float writefln("%.16f", num); // for double
Jul 24 2019
prev sibling next sibling parent reply a11e99z <black80 bk.ru> writes:
On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:
 int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",&num);
  Writeln(num);

 }

 How do I return the complete number the user enter since I 
 don't since the user can enter numbers of various length with 
 dmd approximating it.

 I will appreciate any help
readf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
Jul 24 2019
parent reply a11e99z <black80 bk.ru> writes:
On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:
 int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",&num);
  Writeln(num);

 }

 How do I return the complete number the user enter since I 
 don't since the user can enter numbers of various length with 
 dmd approximating it.

 I will appreciate any help
readf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
oops! i misunderstood
Jul 24 2019
parent reply Greatsam4sure <greatsam4sure gmail.com> writes:
On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure 
 wrote:
 int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",&num);
  Writeln(num);

 }

 How do I return the complete number the user enter since I 
 don't since the user can enter numbers of various length with 
 dmd approximating it.

 I will appreciate any help
readf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
writeln(num) The result is always to six significant figure. I want the whole number to be diaplay
 oops! i misunderstood
Jul 24 2019
next sibling parent a11e99z <black80 bk.ru> writes:
On Wednesday, 24 July 2019 at 16:16:15 UTC, Greatsam4sure wrote:
 On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure
The result is always to six significant figure. I want the whole number to be display

see another answers at thread https://forum.dlang.org/post/qh9v10$19v1$1 digitalmars.com https://forum.dlang.org/post/afzqnnorzwosimfqnbcb forum.dlang.org
Jul 24 2019
prev sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Wednesday, 24 July 2019 at 16:16:15 UTC, Greatsam4sure wrote:
 On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:
 On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure 
 wrote:
 int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",&num);
  Writeln(num);

 }

 How do I return the complete number the user enter since I 
 don't since the user can enter numbers of various length 
 with dmd approximating it.

 I will appreciate any help
readf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
writeln(num) The result is always to six significant figure. I want the whole number to be diaplay
So, there are several possible issues here. First, floating-point numbers have limited precision - a double can not correctly represent all integers above 2^52, for instance. That's 4.5 quadrillion though, so rarely an issue. In your case, as others have commented, writefln with a correctly chosen format string[1] will do the trick. However, what is the correctly chosen format string? This is a very hard question to answer, as it depends on what number was typed, and what you mean by 'the complete number the user enter'. While drug's suggestion of "%.16f" will return an accurate representation of the floating-point number, it will include a lot of trailing zeroes that were presumably not present in the input string. It will also expose floating-point's limitations when presented with huge numbers, like 1e23 will print 99999999999999991603000.0000000000000000. The best option might be "%.16g", which will choose the shortest of the scientific notation and the regular notation, as well as strip trailing zeroes. Depending on the criteria though, this might still not be the optimal solution. -- Simen 1: https://dlang.org/phobos/std_format.html#format-string
Jul 24 2019
prev sibling parent a11e99z <black80 bk.ru> writes:
On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:
 int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",&num);
  Writeln(num);

 }

 How do I return the complete number the user enter since I 
 don't since the user can enter numbers of various length with 
 dmd approximating it.

 I will appreciate any help
as drug said: writefln with formatting in case no need prints just string representation use s/format https://dlang.org/phobos/std_format.html#format auto str = num.format!"%.16s"; // using UFCS or auto str = "%.16s".format( num); // UFCS too for 2nd version of format or string str = format( "%.16s", num); simple help can be asked on IRC channel too irc://irc.freenode.net/d (HexChat, mIRC clients and others)
Jul 24 2019