Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Type conversion

See also std.conv.

(1) type conversion
#include <stdio.h>

int main()
{
    int x = 3;
    int y = 2;

    // double result = x / y;  // integer division

    double result = (double)x / (double)y;
    printf("result: %.2lf\n", result);

    return 0;
}
import std.stdio;
import std.conv;  // to!double

void main()
{
    int x = 3;
    int y = 2;

    // double result = x / y;  // integer division

    double result = double(x) / y;
    // double result = cast(double) x / y; // also works
    // double result = x.to!double / y;  // also works

    writefln("result: %s", result);
}

Output:

result: 1.5

Safe way

We get an exception if the conversion cannot be done.

(2) to!<type>()
def main():
    print(float("3.14"))  # 3.14 , as float
    print(float("6.78"))  # 6.78 , as float
    print(int("42"))  # 42 , as int
    print(str(1983))  # "1983" , as string
import std.stdio;
import std.conv;

void main()
{
    writeln(to!double("3.14")); // 3.14 , as double
    writeln(to!float("6.78")); // 6.78 , as float
    writeln(to!int("42")); // 42 , as int
    writeln(to!string(1983)); // "1983" , as string
}
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 June 27, 12:12