How to get the first digit of float number in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        float f = 872.1459f;
        string s = f.ToString();

        Console.Write(s[0]);
    }
}



/*
run:

8

*/

 



answered Aug 29, 2019 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        float f = 972.1459f;
        int int_part = (int)f;
        int total_digits_minus_one = (int)Math.Log10(int_part);
        int first_digit = (int)(int_part / Math.Pow(10, total_digits_minus_one));
  
        Console.WriteLine(first_digit);
    }
}



/*
run:

9

*/

 



answered Aug 29, 2019 by avibootz

Related questions

...