How to Split Number Into Digit in Windows Store App

Introduction

In this article I explain how to separate each digit from specified number. For example: "5674" is presented in the from of "5 6 7  4"; in other words, I have separated each digit from the number.

Use the  following procedure to split a number into digits.

Step 1

First of all you have to create a new Windows Store Application.

  • Open Visual Studio 2012
  • "File" -> "New" -> "Project..."
  • Choose "Template" -> "Visual C#" -> "Window Store app"
  • Choose "Blank App (XAML)" then rename the application

new-windows-store-app.jpg

Step 2

Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="digit_app.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:digit_app"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="Red">

        <Grid.RowDefinitions>

            <RowDefinition Height="131*"/>

            <RowDefinition Height="44*"/>

            <RowDefinition Height="41*"/>

            <RowDefinition Height="41*"/>

            <RowDefinition Height="36*"/>

            <RowDefinition Height="374*"/>

            <RowDefinition Height="101*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="164*"/>

            <ColumnDefinition Width="128*"/>

            <ColumnDefinition Width="391*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Split Number into Digit " FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="1" ></TextBlock>

        <TextBlock Text="Enter Number:" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="2" ></TextBlock>

        <TextBox x:Name="textbox1" Grid.Column="2" Grid.Row="2" Width="200" Height="32" HorizontalAlignment="Left" VerticalAlignment="Top"></TextBox>

        <Button x:Name="button1" Content="click" Grid.Column="2" Grid.Row="3" Width="104" Click="button1_Click" Background="Yellow" Foreground="Black" Height="38" Margin="0,3,0,0" VerticalAlignment="Top" />

        <TextBlock x:Name="text1" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="2" Grid.Row="4" Width="394" HorizontalAlignment="Left"  ></TextBlock>

    </Grid>

 

</Page>

 

Step 3

Now write the following C# code for the button within "Mainpage.Xaml.cs".

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace digit_app

{  

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            int num = 0, temp;

            int factor = 1;       

            num = Convert.ToInt32(textbox1.Text);

            temp = num;

            while (temp  > 0)

            {

                temp = temp / 10;             

                factor = factor * 10;               

            }

            while (factor > 1)

            {

                factor = factor / 10;

                text1.Text += (num / factor) + " ";

                num = num % factor;

            }

        }

    }

}

 

Step 4

Now run your app.

run-digit-app.jpg

output-of-digit-app.jpg