digitalmars.D.learn - Return the complete number
- Greatsam4sure (10/10) Jul 24 2019 int main(){
- drug (4/16) Jul 24 2019 does it help?
- a11e99z (6/16) Jul 24 2019 readf!" %s\n"( num );
- a11e99z (2/21) Jul 24 2019 oops! i misunderstood
- Greatsam4sure (4/27) Jul 24 2019 writeln(num)
- a11e99z (4/10) Jul 24 2019 see another answers at thread
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (23/49) Jul 24 2019 So, there are several possible issues here. First, floating-point
- a11e99z (13/23) Jul 24 2019 as drug said: writefln with formatting
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
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 helpdoes it help? writefln("%.8f", num); // for float writefln("%.16f", num); // for double
Jul 24 2019
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 helpreadf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
Jul 24 2019
On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:oops! i misunderstoodint 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 helpreadf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
Jul 24 2019
On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:writeln(num) The result is always to six significant figure. I want the whole number to be diaplayOn 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 helpreadf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;oops! i misunderstood
Jul 24 2019
On Wednesday, 24 July 2019 at 16:16:15 UTC, Greatsam4sure wrote:On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:see another answers at thread https://forum.dlang.org/post/qh9v10$19v1$1 digitalmars.com https://forum.dlang.org/post/afzqnnorzwosimfqnbcb forum.dlang.orgOn Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:The result is always to six significant figure. I want the whole number to be displayOn Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure
Jul 24 2019
On Wednesday, 24 July 2019 at 16:16:15 UTC, Greatsam4sure wrote:On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote: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-stringOn Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:writeln(num) The result is always to six significant figure. I want the whole number to be diaplayOn 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 helpreadf!" %s\n"( num ); or num.readf!" %s\n"; or num = readln.strip.to!double;
Jul 24 2019
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 helpas 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